[Tutor] List comprehension

Kirby Urner urnerk@qwest.net
Wed, 23 Jan 2002 10:30:18 -0800


>
>But, EEK! Why did you need the eval there??
>
>[x(y) for x in [lambda x:x*x, lambda x:x*x*x] for y in [1,2,3]]
>
>works fine as well.

Good point!  I was being unnecessarily obscure.

Along same lines, we can iterate over predefined functions
(in place of lambdas):

  >>> def f(x): return x*x

  >>> def g(x): return x*x*x

  >>> [k(x) for k in [f,g] for x in [1,2,3,4]]
  [1, 4, 9, 16, 1, 8, 27, 64]

Kirby