>>> feast = ['lambs', 'sloths', 'orangutans', 'breakfast cereals', 'fruit bats']>>> comprehension = [delicacy.capitalize() for delicacy in feast]
>>> comprehension[0] ???>>> comprehension[2] ???
>>> feast = ['spam', 'sloths', 'orangutans', 'breakfast cereals', 'fruit bats']>>> comprehension = [delicacy for delicacy in feast if len(delicacy) > 6]
>>> len(feast) ???>>> len(comprehension) ???
>>> list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')]>>> comprehension = [ skit * number for number, skit in list_of_tuples ]
>>> comprehension[0] ???>>> len(comprehension[2]) ???
>>> list_of_eggs = ['poached egg', 'fried egg']>>> list_of_meats = ['lite spam', 'ham spam', 'fried spam']>>> comprehension = [ '{0} and {1}'.format(egg, meat) for egg in list_of_eggs for meat in list_of_meats]
>>> len(comprehension) ???>>> comprehension[0]
>>> comprehension = { x for x in 'aabbbcccc'}
>>> comprehension ???
>>> dict_of_weapons = {'first': 'fear', 'second': 'surprise', 'third':'ruthless efficiency', 'forth':'fanatical devotion', 'fifth': None}>>> dict_comprehension = { k.upper(): weapon for k, weapon in dict_of_weapons.iteritems() if weapon}
>>> 'first' in dict_comprehension
???
>>> 'FIRST' in dict_comprehension ???>>> len(dict_of_weapons) ???>>> len(dict_comprehension) ???
See also: https://github.com/gregmalcolm/python_koans https://github.com/gregmalcolm/python_koans/blob/master/python2/koans/about_comprehension.py
This is from CodingBat "count_evens" (http://codingbat.com/prob/p189616)
Using list comprehension, return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
count_evens([2, 1, 2, 3, 4]) → 3
count_evens([2, 2, 0]) → 3
count_evens([1, 3, 5]) → 0
def count_evens(nums):