#### Python List Comprehension Flashcards

card 1 of 9

[i*i for i in range(3)]

list comprehension generates a new list

[0,1,4]

card 2 of 9

[(i,True) for i in range(3)]

the item in the new list is derived from the term before "for"

[(0,True),(1,True),(2,True)]

card 3 of 9

[ myTerm for myTerm in 'iowa city']

consider list('iowa city') as the source for myTerm substitution

['i','o','w','a',' ','c','i','t','y']

card 4 of 9

[ letter for letter in "Mississipi" if letter != 'i']

the "if" part makes exceptions for including with output

['M','s','s','s','s','p']

card 5 of 9

[ e for e in "restore" if e not in "stereo"]

the "if" part makes exceptions for including with output

[ ]

card 6 of 9

[ a+b for a in 'back' for b in 'top']

evaluate a+b under all possible conditions allowed

['bt','bo','bp','at','ao','ap','ct','co','cp','kt','ko','kp']

card 7 of 9

[ bool(x) for x in [3,True,'',0.0]]

remember how conversion to boolean works

[True,True,False,False]

card 8 of 9

max( [ len(x) for x in "counting word size is easy".split() ] )

max calculates maximum of a sequence; evaluate split first

8

card 9 of 9

[i*i for i in list(range(8))+list(range(12))].count(9)

first concatenate the two range lists, then get new list, and finally apply the count method

2

#### End of Problem Set

(use browser reload to restart this problem set)