index()-like behavior that returns all occurences...
pj at sgi.com
Wed Dec 19 23:39:11 CET 2001
|> x = [ , [1,2], , [1,2,3], , ,  ]
|> I want to find the index of all of the empty lists. In other words, I'd
|> like a function foo that would behave like this:
|> >> foo(x)
|> [2 4]
Well, you could do a one liner with the lambda, map, zip, filter routines:
map (lambda x: x, filter (lambda x: not x, zip (range(len(x)), x)))
But this has _got_ to be less readable than the simple for loop,
even to whomever it was that championed getting map, reduce and
filter into the Python language in the first place.
I won't rest till it's the best ...
Manager, Linux Scalability
Paul Jackson <pj at sgi.com> 1.650.933.1373
More information about the Python-list