set and dictionary comprehensions the syntax expr for in if conditionalis set comprehension it applies an operation to all of the elements of set and can be used in similar manner as list comprehensions for examplevalues squares { * for in valuessquares { the syntax kexpr:vexpr for , in if condition is dictionary comprehension it applies an operation to all of the keys and values in sequence of (keyvaluetuples and returns dictionary the keys of the new dictionary are described by an expression kexprand the values are described by the expression vexpr this can be viewed as more powerful version of the dict(function to illustratesuppose you had file of stock prices 'prices datlike thisgoog yhoo ibm msft aapl here is program that reads this file into dictionary mapping stock names to price using dictionary comprehensionfields (line split(for line in open("prices dat")prices {sym:float(valfor sym,val in fieldshere is an example that converts all of the keys of the prices dictionary to lowercased {sym lower():price for sym,price in prices items()here is an example that creates dictionary of prices for stocks over $ {sym:price for sym,price in prices items(if price > extended iterable unpacking in python items in an iterable can be unpacked into variables using syntax such as the followingitems [ , , , , , , items unpack items into variables in order for this unpacking to workthe number of variables and items to be unpacked must exactly match in python you can use wildcard variable to only unpack some of the items in sequenceplacing any remaining values in list for examplea,*rest items ,*rest, items *restd items rest [ , , rest [ , ] rest [ , , ] lib fl ff