Linked Questions

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 ...
Jonathan Livni's user avatar
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 ...
fortran's user avatar
  • 75.4k
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]...
Salil's user avatar
  • 9,692
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. ...
Alexander Tobias Bockstaller's user avatar
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 ...
David542's user avatar
  • 108k
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 ...
Jough Dempsey's user avatar
-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 ...
J Mo's user avatar
  • 43
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': ...
user4812479812's user avatar
-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?
Gaith Mtiri's user avatar
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 ...
Kudrllr's user avatar
  • 19
-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 ...
SUSIE's user avatar
  • 1
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 ...
S L SREEJITH's user avatar
-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]....
Basj's user avatar
  • 44.9k
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 ...
Ivan's user avatar
  • 1,467
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 ...
Core work's user avatar

15 30 50 per page