# [Python-Dev] list comprehension quiz

Greg Wilson gvwilson@nevex.com
Tue, 25 Jul 2000 23:08:49 -0400 (EDT)

```Hi, folks.  In order to put the list comprehension syntax questionnaire
together, I would be grateful if anyone with a favorite syntax could send
me their equivalent of the following scraps of Python (or other scraps and
their equivalents, if you think there are interesting cases that I'm not
covering).

In order to save bandwidth, please mail me directly --- I will summarize
and post the questionnaire itself.

Thanks,
Greg

-- 1 ----------------------------------------

input  = [1, 2, 3, 4, 5]
output = []
for num in input:
output.append(2 * num)
>>> [2, 4, 6, 8, 10]

-- 2 ----------------------------------------

input  = [1, 2, 3, 4, 5]
output = []
for num in input:
if is_odd(num):
output.append(num)
>>> [1, 3, 5]

-- 3 ----------------------------------------

input = [1, 2, 3, 4, 5]
output = 0
for num in input:
output = output + num
>>> 15

-- 4 ----------------------------------------

tens   = [10, 20, 30]
units  = [ 1,  2]
output = []
for t in tens:
for u in units:
output.append(u + t)
>>> [11, 12, 21, 22, 31, 32]

-- 5 ----------------------------------------

tens   = [10, 20, 30]
units  = [ 1,  2,  3,  4,  5]
output = []
lim    = min(len(tens), len(units))
for i in range(0, lim):
output.append(max(tens[i], units[i]))
>>> [10, 20, 30]

-- 6 ----------------------------------------

phones = {229 : 'Greg', 231 : 'Toni',
268 : 'Jaq',  271 : 'Hassan'}
output = []
for p in phones.keys():
if p > 250:
output.append(phones[p])
>>> ['Hassan', 'Jaq']

```