how to test for nesting inlist

Remco Gerlich scarblac at pino.selwerd.nl
Mon Jan 22 12:54:04 EST 2001


cpsoct at my-deja.com <cpsoct at my-deja.com> wrote in comp.lang.python:
> What are list comprehensions? I have looked and looked and looked in
> books and on www.python.org and i see no mention of these. Is this
> something new? or i am i just getting tripped up by the jargon and list
> compreshensions are something i already know by another name? I am
> confused. Hmm.....

They're new in Python 2.0, they are inspired by languages like Haskell.

>>> [ord(x) for x in "SPAM"]
[83, 80, 65, 77]

>>> [3*x for x in range(5)]
[0, 3, 6, 9, 12]

>>> [x for x in range(10) if x%2==0]
[0, 2, 4, 6, 8]

>>> [(x,y) for x in range(2) for y in range(3)]
[(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]

>>> [x for x in zip("SPAM", "EGGS")]
[('S','E'), ('P', 'G'), ('A','G'), ('M','S')]

There must be docs on it somewhere, but you can get a feel for it playing
with examples, I think...

-- 
Remco Gerlich






More information about the Python-list mailing list