filtering elements from list of lists in Python?
Posted
by user248237
on Stack Overflow
See other posts from Stack Overflow
or by user248237
Published on 2010-04-16T20:37:40Z
Indexed on
2010/04/16
20:43 UTC
Read the original article
Hit count: 241
I want to filter elements from a list of lists, and iterate over the elements of each element using a lambda. For example, given the list:
a = [[1,2,3],[4,5,6]]
suppose that I want to keep only elements where the sum of the list is greater than N. I tried writing:
filter(lambda x, y, z: x + y + z >= N, a)
but I get the error:
<lambda>() takes exactly 3 arguments (1 given)
How can I iterate while assigning values of each element to x, y, and z? Something like zip, but for arbitrarily long lists.
thanks,
p.s. I know I can write this using: filter(lambda x: sum(x)..., a) but that's not the point, imagine that these were not numbers but arbitrary elements and I wanted to assign their values to variable names.
© Stack Overflow or respective owner