Can I do this with list comprehension?

Erik Max Francis max at alcyone.com
Wed Aug 23 00:27:36 EDT 2006


barberomarcelo at gmail.com wrote:

> Let's say I have two lists:
> 
> a = [0, 1, 0, 1, 1, 0]
> b = [2, 4, 6, 8, 10, 12]
> 
> I want a list comprehension that has the elements in b where a[element]
> == 1.
> 
> That's to say, in the example above, the result must be: [4, 8, 10]
> 
> Any hints?

 >>> from itertools import izip
 >>> a = [0, 1, 0, 1, 1, 0]
 >>> b = [2, 4, 6, 8, 10, 12]
 >>> [y for x, y in izip(a, b) if x == 1]
[4, 8, 10]


-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Man is a hating rather than a loving animal.
    -- Rebecca West



More information about the Python-list mailing list