You can use the filter method:
>>> lst = [1, 2, 3, 4, 5]
>>> filter(lambda x: x % 2 == 0, lst)
[2, 4]
or a list comprehension:
>>> lst = [1, 2, 3, 4, 5]
>>> [x for x in lst if x %2 == 0]
[2, 4]
to find a single element, you could try:
>>> next(x for x in lst if x % 2 == 0)
2
Though that would throw an exception if nothing matches, so you'd probably want to wrap it in a try/catch. The () brackets make this a generator expression rather than a list comprehension.
Personally though I'd just use the regular filter/comprehension and take the first element (if there is one).
These raise an exception if nothing is found
filter(lambda x: x % 2 == 0, lst)[0]
[x for x in lst if x %2 == 0][0]
These return empty lists
filter(lambda x: x % 2 == 0, lst)[:1]
[x for x in lst if x %2 == 0][:1]
Answer from John Montgomery on Stack OverflowYou can use the filter method:
>>> lst = [1, 2, 3, 4, 5]
>>> filter(lambda x: x % 2 == 0, lst)
[2, 4]
or a list comprehension:
>>> lst = [1, 2, 3, 4, 5]
>>> [x for x in lst if x %2 == 0]
[2, 4]
to find a single element, you could try:
>>> next(x for x in lst if x % 2 == 0)
2
Though that would throw an exception if nothing matches, so you'd probably want to wrap it in a try/catch. The () brackets make this a generator expression rather than a list comprehension.
Personally though I'd just use the regular filter/comprehension and take the first element (if there is one).
These raise an exception if nothing is found
filter(lambda x: x % 2 == 0, lst)[0]
[x for x in lst if x %2 == 0][0]
These return empty lists
filter(lambda x: x % 2 == 0, lst)[:1]
[x for x in lst if x %2 == 0][:1]
Generators and list comprehensions are more pythonic than chainable functions.
>>> lst = [i for i in range(1, 6)]
>>> lst
[1, 2, 3, 4, 5]
>>> gen = (x for x in lst if x % 10 == 0)
>>> next(gen, 'not_found')
'not_found'
>>> [x for x in gen]
[]
For example, I use it like this sometimes:
>>> n = next((x for x in lst if x % 10 == 0), None)
>>> if n is None:
... print('Not found')
...
Not found
Otherwise, you can define your utility function oneliners like this:
>>> find = lambda fun, lst: next((x for x in lst if fun(x)), None)
>>> find(lambda x: x % 10 == 0, lst)
>>> find(lambda x: x % 5 == 0, lst)
5
>>> findall = lambda fun, lst: [x for x in lst if fun(x)]
>>> findall(lambda x: x % 5 == 0, lst)
[5]
How to return a list of lambda function?
How can I find a value in a list using Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
Python: Fetch item in list where dict key is some value using lambda - Stack Overflow
python - Using lambda function to find element from another list or array - Stack Overflow
Using lambda here isn't necessary. Lambda isn't something magical, it's just a shorthand for writing a simple function. It's less powerful than an ordinary way of writing a function, not more. (That's not to say sometimes it isn't very handy, just that it doesn't have superpowers.)
Anyway, you can use a generator expression with the default argument. Note that here I'm returning the object itself, not 20, because that makes more sense to me.
>>> somelist = [{"id": 10, "x": 1}, {"id": 20, "y": 2}, {"id": 30, "z": 3}]
>>> desired_val = next((item for item in somelist if item['id'] == 20), None)
>>> print(desired_val)
{'y': 2, 'id': 20}
>>> desired_val = next((item for item in somelist if item['id'] == 21), None)
>>> print(desired_val)
None
Using a lambda as you asked, with a generator expression, which is generally considered more readable than filter, and note this works equally well in Python 2 or 3.
lambda x: next(i for i in x if i['id'] == 20)
Usage:
>>> foo = lambda x: next(i for i in x if i['id'] == 20)
>>> foo(x)
{'Car': 'Toyota', 'id': 20}
And this usage of lambda is probably not very useful. We can define a function just as easily:
def foo(x):
return next(i for i in x if i['id'] == 20)
But we can give it docstrings, and it knows its own name and has other interesting attributes that anonymous functions (that we then name) don't have.
Additionally, I really think what you're getting at is the filter part of the expression.
In
filter(lambda x: x[id]==20, x)
we have replaced that functionality with the conditional part of the generator expression. The functional part of generator expressions (list comprehensions when in square brackets) are similarly replacing map.