Linked Questions
57 questions linked to/from Get the first item from an iterable that matches a condition
407
votes
2
answers
318k
views
Find first sequence item that matches a criterion [duplicate]
What would be the most elegant and efficient way of finding/returning the first list item that matches a certain criterion?
For example, if I have a list of objects and I would like to get the first ...
284
votes
4
answers
200k
views
Find first element in a sequence that matches a predicate [duplicate]
I want an idiomatic way to find the first element in a list that matches a predicate.
The current code is quite ugly:
[x for x in seq if predicate(x)][0]
I've thought about changing it to:
from ...
33
votes
4
answers
13k
views
Sequence find function? [duplicate]
How do I find an object in a sequence satisfying a particular criterion?
List comprehension and filter go through the entire list. Is the only alternative a handmade loop?
mylist = [10, 2, 20, 5, 50]...
16
votes
4
answers
25k
views
Finding the first list element for which a condition is true [duplicate]
I was looking for an elegant (short!) way to return the first element of a list that matches a certain criteria without necessarily having to evaluate the criteria for every element of the list. ...
4
votes
2
answers
10k
views
How to get first non-null item in list [duplicate]
How would I grab the following:
l=[None, None, 'hello', 'hello']
first(l) ==> 'hello'
l = [None, None, None, None]
first(l) ==> None
I could try doing it with a list comprehension, but that ...
1
vote
7
answers
7k
views
Finding a Value within a Range in a List of Tuple Values in Python [duplicate]
I'm trying to get the Body Mass Index (BMI) classification for a BMI value that falls within a standard BMI range - for instance, if someone's BMI were 26.2, they'd be in the "Overweight" range.
I ...
-1
votes
1
answer
2k
views
Check if an object has an attribute from a list of attributes, and if is found assign it to a variable - dynamic [duplicate]
I check if an object has an attribute or another, can have only one.
If the attribute is found, assign his value to a variable. Can this be done dynamic(the attributes number can variate), getting ...
1
vote
1
answer
808
views
Filter dictionary in list by value [duplicate]
>>> my_list = [{u'name': u'name1', u'color': u'red'}, {u'name': u'name2', u'color': u'blue'}]
>>> my_list[0]
{u'color': u'red', u'name': u'name1'}
>>> my_list[1]
{u'color': ...
-5
votes
1
answer
1k
views
How to get a specific item from python list [duplicate]
class Barcode:
def __init__(self, code, code_id = None):
self.code = code
self.id = code_id
I have a list of barcodes. How can I get a barcode with a specific id?
0
votes
4
answers
358
views
Python - How to return the position in string where character matches from a list [duplicate]
I have a list:
symbol_list = ['/', '.', '\"', '-']
and a changing string which currently contains:
string = 'This is a string/ of "text"'
and I'm trying to find the most efficient way to return ...
-4
votes
1
answer
308
views
Create a sublist that only contains all the numbers of a list until it reaches a certain number [duplicate]
I want to write a code , that contains a while loop that only stops once the element of the list is the number 9. What is returned is a list of all of the numbers up until it reaches 9. So what it ...
0
votes
1
answer
211
views
How to get the first occurence of the element in the list from a given string? [duplicate]
string = 'get selected items from the list'
lst_keywords = ['list', 'selected', 'python', 'subset']
res = [ele for ele in lst_keywords if(ele in string)]
then res = ['list', 'selected']
But I ...
-1
votes
2
answers
46
views
Find a value in a list of dicts [duplicate]
Let:
M = [{'name': 'john', 'result': 12},
{'name': 'sara', 'result': 20},
{'name': 'karl', 'result': 11}]
If I want to find Sara's result, I thought about:
M[[m['name'] for m in M]....
0
votes
2
answers
100
views
What's the quickest way to get the first match by an object attribute in a Python list? [duplicate]
I have a line of code like so:
event = [x for x in history if x.serial == serialized_event]
In all circumstances in my application, the resulting list will only ever contain a single item. But it ...
0
votes
2
answers
100
views
How to find the word which it had found in string from list of words [duplicate]
How can we know which word is present by using this??
word_list = ['hello','hi','super']
if any(word in string for word in word_list):
print('found')
By this I’m able to iterate through the ...