index()-like behavior that returns all occurences...

Skip Montanaro skip at pobox.com
Wed Dec 19 17:46:49 EST 2001


    Lorin> x = [ [1], [1,2], [], [1,2,3], [], [2], [12] ]

    [i for i in range(len(x)) if len(x[i]) == 0]

    Lorin> I want to find the index of all of the empty lists. 

List comprehensions are a pretty straightforward way to deal with this
problem: 

>>> x = [ [1], [1,2], [], [1,2,3], [], [2], [12] ]
>>> [i for i in range(len(x)) if len(x[i]) == 0]
[2, 4]

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list