If you don't have any other indexes or sorted information for your objects, then you will have to iterate until such an object is found:

next(obj for obj in objs if obj.val == 5)

This is however faster than a complete list comprehension. Compare these two:

[i for i in xrange(100000) if i == 1000][0]

next(i for i in xrange(100000) if i == 1000)

The first one needs 5.75ms, the second one 58.3µs (100 times faster because the loop 100 times shorter).

Answer from eumiro on Stack Overflow
🌐
Real Python
realpython.com › python-first-match
How to Get the First Match From a Python List or Iterable – Real Python
May 28, 2023 - Python generator iterators are memory-efficient iterables that can be used to find the first element in a list or any iterable. They’re a core feature of Python, being used extensively under the hood.
Discussions

python - Find first element in a sequence that matches a predicate - Stack Overflow
This rather popular article further discusses this issue: Cleanest Python find-in-list function?. More on stackoverflow.com
🌐 stackoverflow.com
Help finding first instance of a element in tuple/list
You have a weird data format. Your line a = ([[5], [4, 5], [4], [3, 4], [3], [1, 3], [1], [3, 7], [7], [1, 7], [1, 8], [8], [2, 7], [2], [2, 6], [6]],) Is actually a tuple containing 2 items. A list containing lists containing integers, None all because of that comma at the end! So if we call that tuple a: answer = [item for item in a[0] if item in [[7],[8],[9]]] More on reddit.com
🌐 r/learnpython
4
1
October 13, 2022
Help operating in a smart way to make lists of first element in lists of list.
By list of list, I assume you mean vector of vectors. Here's dummy version (I'll use smaller input data, for demonstration): julia> vecvec = [rand(0:9, 3) for _ in 1:10]; 10-element Vector{Vector{Int64}}: [8, 8, 4] [9, 3, 0] [2, 4, 6] [7, 0, 9] [6, 8, 4] [3, 7, 6] [8, 2, 7] [3, 0, 2] [7, 6, 8] [7, 2, 9] The getindex function reads elements from indexable collections, like vectors. The trick here is to broadcast it over your outer vector, using a dot: julia> getindex.(vecvec, 1) 10-element Vector{Int64}: 8 9 2 7 6 3 8 3 7 7 It works similarly for index 2, etc. You can go further, and also broadcast over the indices you are interested in, like this: julia> getindex.(vecvec, (1:3)') 10×3 Matrix{Int64}: 8 8 4 9 3 0 2 4 6 7 0 9 6 8 4 3 7 6 8 2 7 3 0 2 7 6 8 7 2 9 Notice the prime in (1:3)'. This is necessary to make sure that vecvec and the indices stretch out in different directions: vecvec downwards (column) and indices as a row. Then the dimensions are broadcasted over to get the right output shape. You can also use transpose or permutedims for this, the latter allows reshaping in higher dimensions. More on reddit.com
🌐 r/Julia
14
3
February 3, 2023
How to get first empty string in a list in Python
So the contact book is supposed to have a limited number of entries, and if you reach the limit, you need to overwrite an entry? Is that desired? Because it could be easily avoidable More on reddit.com
🌐 r/learnprogramming
10
4
September 5, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-first-and-last-elements-of-a-list
Get first and last elements of a list in Python - GeeksforGeeks
Explanation: first, *_, last = a assigns the first element (1) to first, the last element (4) to last, and the middle elements [5, 6, 7] to _, which is ignored as _ is a placeholder for unused values.
Published   July 11, 2025
🌐
LabEx
labex.io › tutorials › python-how-to-find-first-matching-element-in-python-421206
How to find first matching element in Python | LabEx
Python provides multiple approaches to find the first matching element in a collection: graph TD A[Start Search] --> B{Element Found?} B -->|Yes| C[Return Element] B -->|No| D[Continue Searching] D --> E[End of Collection] E --> F[Return None/Raise Exception] ## Simple list search numbers = [1, 2, 3, 4, 5, 6, 7] ## Find first even number first_even = next((num for num in numbers if num % 2 == 0), None) print(first_even) ## Output: 2 ## LabEx Tip: Efficient searching saves computational resources
🌐
Code-maven
python.code-maven.com › python-find-first-element-in-list-matching-condition
Find the first element in Python list that matches some condition
animals = ['snake', 'camel', 'etruscan shrew', 'ant', 'hippopotamus', 'giraffe'] first = next(filter(lambda animal: len(animal) > 5, animals), None) print(first) print('-------') filtered_list = filter(lambda animal: len(animal) > 5, animals) print(filtered_list) first = next(filtered_list, None) print(first) ... In Python 2 filter would have returned a list already checking all the values of the original list. That would be a waste of time and CPU. The filter function in Python 3 returns a filter object and only executes the filtering condition when the user actually wants to find the next element in the filtered list.
🌐
LabEx
labex.io › tutorials › python-how-to-locate-first-occurrence-in-lists-464735
Python - How to locate first occurrence in lists
Python uses zero-based indexing, meaning the first element is at index 0. fruits = ['apple', 'banana', 'cherry', 'date'] print(fruits[0]) ## Outputs: apple print(fruits[2]) ## Outputs: cherry · Negative indices allow accessing elements from ...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-get-first-item-in-list-that-matches-condition
Get the first item in a List that matches Condition - Python | bobbyhadz
Use the any() function to find the first item that matches a condition. Use the assignment expression syntax to assign the first match to a variable. main.py · Copied!my_list = [1, 3, 7, 14, 29, 35, 105] if any((match := item) > 29 for item ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-first-occurrence-of-one-list-in-another
Python - First Occurrence of One List in Another - GeeksforGeeks
July 23, 2025 - A generator expression with next() provides an efficient way to find the first match without iterating the entire list. ... # Initializing lists a = [1, 2, 3, 4, 5, 6] b = [3, 4, 5] # Finding first occurrence using next() index = next((i for ...
🌐
datagy
datagy.io › home › python posts › python list index: find first, last or all occurrences
Python List Index: Find First, Last or All Occurrences • datagy
February 28, 2022 - By the end of this tutorial, you’ll have learned: ... The Python list.index() method returns the index of the item specified in the list. The method will return only the first instance of that item.
🌐
Adam.Blog()
adampresley.github.io › 2012 › 11 › 06 › python-list-comprehension-getting-the-first-matching-item.html
Python List Comprehension - Getting The First Matching Item
November 6, 2012 - a = [1, 2, 3, 4, 5] def findFirstNumberGreaterThan2(): return next((i for i in a if i > 2), None) Woah, that is pretty cool! The next() method takes two arguments. The first being an object that can be iterated over, and the second being a default value that is returned once the iterator is exhausted. The second argument is optional. I definitely need to spend more time digging into the “pythonic” tricks of the trade.
🌐
Quora
quora.com › How-do-you-get-the-first-value-in-a-list-in-Python
How to get the first value in a list in Python - Quora
For example you have created a list named as L1 and you want to know the first element So you can just do this in python terminal >>>L1[0] ... Former Systems Programmer, Chief Designer (1982–2021) · Author has 3.6K answers and 1.4M answer views · 3y · Originally Answered: What is the easiest way to find ...
🌐
How to Use Linux
howtouselinux.com › home › how to get the first element of a list in python
How to get the first element of a list in Python - howtouselinux
October 9, 2025 - Because a list usually contains ... of numbers called nums: nums = [0, -12, 17, 99] The best way to get the first element of a list in Python is using the list[0]. Python lists are zero-indexed....
🌐
Medium
medium.com › swlh › python-the-fastest-way-to-find-an-item-in-a-list-19fd950664ec
Python: The Fastest Way to Find an Item in a List | by Sebastian Witowski | The Startup | Medium
September 25, 2020 - We can use a generator expression to grab the first element matching our criteria. The whole code looks very similar to a list comprehension, but we can actually use count. Generator expression will execute only enough code to return the next ...
🌐
Gitbook
sisyphus.gitbook.io › project › python-notes › python-find-first-value-index-in-a-list-list-.index-val
Python find first value index in a list: [list].index(val) | The Truth of Sisyphus
August 11, 2018 - >>> print(list.index.__doc__) L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. An index call checks every element of the list in order, until it finds a match. If your list is long, and you don't know roughly where in the list it occurs, this search could become a bottleneck.
🌐
AskPython
askpython.com › python › list › get-first-last-elements-python-list
How to Get the First and Last Elements of a Python List? - AskPython
April 5, 2023 - The default index starts from 0 and goes on till n-1. Here n denotes the total number of elements in the respective data structure. You can access the first element from the list by using the name of the list along with index 0.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-first-occurrence-of-true-number
Python - First Occurrence of True number - GeeksforGeeks
July 11, 2025 - For example we are having a list a = [False, False, True, False, True] so we need to return first occurrence of True so that output should be 2 in this case. index() method in Python returns index of first occurrence of a specified value in a list.
🌐
GitHub
gist.github.com › huynhsamha › c7122b82c131f07b444b868dbce50473
[Python 3] - Find first match predicate in Python · GitHub
[Python 3] - Find first match predicate in Python · Raw · FindFirst.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Reddit
reddit.com › r/learnpython › help finding first instance of a element in tuple/list
r/learnpython on Reddit: Help finding first instance of a element in tuple/list
October 13, 2022 -

Hello everyone, I'm currently working with graphs on python and I'm trying to implement a program for school where I can find all the roads to certain nodes and print the node found first. I'm stuck on this last part specifically. After doing my function I finally get my roadmap as such:

([[5], [4, 5], [4], [3, 4], [3], [1, 3], [1], [3, 7], [7], [1, 7], [1, 8], [8], [2, 7], [2], [2, 6], [6]],)

Where I was looking for the roads starting from 5 to 6,7 and 8. As you can see here I found [7] first.

My question is : how do I print only the first value found out of a list of 3 or more numbers? In this case in particular it would be 7 (notice I only want the instance where 7 is alone, not [3,7] or [1,7]

I already tried:

item for item in tuple if item==7 or item==8 or item==9

to no avail

Thank you!

Top answer
1 of 2
2
You have a weird data format. Your line a = ([[5], [4, 5], [4], [3, 4], [3], [1, 3], [1], [3, 7], [7], [1, 7], [1, 8], [8], [2, 7], [2], [2, 6], [6]],) Is actually a tuple containing 2 items. A list containing lists containing integers, None all because of that comma at the end! So if we call that tuple a: answer = [item for item in a[0] if item in [[7],[8],[9]]]
2 of 2
2
To look at just the first item in a tuple, you can use item[0]. Ie given the above list: [item for item in lst if item[0] in [5,6,7,8]] Will get you all items where the first value is 5, 6, 7 or 8. (You could also write it as item[0] == 5 or item[0] == 6 or ... as you did, but using an in check is more convenient for things like that.) There's a slight complication if your list here can have empty values, since item[0] on an empty list will raise an exception. If you need to handle that case, you could do something like: [item for item in lst if item and item[0] in [5,6,7,8]] (The if item will guard against empty items, so it'll never go on to the next check and try to access the first item) I already tried: item for item in tuple if item==7 or item==8 or item==9 This is failing because item isn't 7, it's a list that may start with 7. If you were just looking for 7 on its own etc, you could do if item == [7]. Also, just to check: your tuple here actually has an extra level of nesting compared to this check. Ie. it's a tuple containing a single list, which contains lists of numbers - above I've been assuming you're iterating over that inner list, not the outer tuple.