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
Discussions

How can I find the index of the first occurrence of a value in a NumPy array? - Python - Data Science Dojo Discussions
I have a NumPy array, and I want to find the index of the first occurrence of a specific element in it. Is there a built-in NumPy function to do this, or do I need to write my own function? Here is an example of my array: import numpy as np arr = np.array([4, 2, 3, 1, 4]) I want to find the ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
1
0
May 8, 2023
python 3.x - Find the First Instances of all Values in a Column of a Numpy Array - Stack Overflow
I'm trying to find the first occurrence of any row in an array in which either column has a number that has changed since the last time it appeared. Given the array below: import numpy as np arr =... 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
Python: Find index of first instance of zero in an array, if none are found return None - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... It returns the same thing although the array ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How can I find the index of the first occurrence of a value in a NumPy array? - Python - Data Science Dojo Discussions
May 8, 2023 - Here is an example of my array: import numpy as np arr = np.array([4, 2, 3, 1, 4]) I want to find the index of the first occurrence of the value 4, which should be 0. I tried using the numpy.where() function, but it returns a tuple of arrays ...
🌐
Real Python
realpython.com › python-first-match
How to Get the First Match From a Python List or Iterable – Real Python
May 28, 2023 - Later in the tutorial, you’ll implement your own variation of the first() function. But first, you’ll look into another way of returning a first match: using generators. ... Python generator iterators are memory-efficient iterables that ...
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-find-the-index-of-the-first-occurrence-of-the-specified-item-in-the-array
Python Array index() Method
May 29, 2023 - The Python array index() method returns smallest index value of the first occurrence of element in an array. Following is the syntax of the Python array index method − This method accepts following parameters.
🌐
Delft Stack
delftstack.com › home › howto › numpy › index of element in array
How to Find the First Index of Element in NumPy Array | Delft Stack
March 11, 2025 - One of the simplest and most effective methods to find the first index of an element in a NumPy array is by utilizing the np.where() function. This function returns the indices of elements that satisfy a given condition.
🌐
PREP INSTA
prepinsta.com › home › data structures and algorithms in python › first occurrence in a sorted array
First Occurrence in a Sorted Array | PrepInsta
July 10, 2025 - The first occurrence of 4 is at index 3. Performs binary search on a sorted array to find the target element.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-index-of-first-occurrence-when-an-unsorted-array-is-sorted
Find index of first occurrence when an unsorted array is sorted - GeeksforGeeks
July 11, 2025 - # Python3 program to find index of first # occurrence of x when array is sorted. import math def findFirst(arr, n, x): arr.sort() # lower_bound returns iterator pointing to # first element that does not compare less # to x. ptr = lowerBound(arr, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-first-occurrence-of-true-number
Python - First Occurrence of True number - GeeksforGeeks
February 4, 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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python-first-occurrence-of-one-list-in-another
Python – First Occurrence of One List in Another | GeeksforGeeks
January 31, 2025 - We are given two lists, and our task is to find the first occurrence of the second list as a contiguous subsequence in the first list. If the second list appears as a subsequence inside the first list, we return the starting index of its first occurrence; otherwise, we return -1. For example, if a = [1, 2, 3, 4, 5, 6] and b = [3, 4, 5], the result should be 2 because [3, 4, 5] starts at index 2 in a. Let's discuss different methods to do this in Python.
🌐
IncludeHelp
includehelp.com › python › numpy-find-first-index-of-value-fast.aspx
Python - NumPy: Find first index of value fast
# Import numpy import numpy as np # Creating an array arr = np.array([1,0,5,0,9,0,4,6,8]) # Display original array print("Original Array:\n",arr,"\n") # Finding index of a value ind = arr.view(bool).argmax() res = ind if arr[ind] else -1 # Display result print("Result:\n",res,"\n")
🌐
AskPython
askpython.com › home › finding an object in a python array – find the first, last, and all occurences of objects in an array
Finding an Object in a Python Array - Find the First, Last, and All Occurences of Objects in an Array - AskPython
August 4, 2021 - For Example: Array Given ==> [1,2,3,4,2] First Occurence ==> 2 · To find the solution to the problem we would take the following steps: Step 1 : Check if list is empty then return that list is empty Step 2 : Check if there is only one element then check the first element with X and return the answer if found Step 3 : For more than one element, we will check if the first element is equal to X if found then return Step 4 : Otherwise recursively go by slicing the array and incrementing and decremementing the itrerator and n value (size of array ) respectively Step 5 : Repeat until the element is found or not
🌐
Javatpoint
javatpoint.com › first-occurrence-using-binary-search-in-python
First Occurrence Using Binary Search in Python - Javatpoint
First Occurrence Using Binary Search in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
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 ...
🌐
Techie Delight
techiedelight.com › home › divide & conquer › find the first or last occurrence of a given number in a sorted array
Find the first or last occurrence of a given number in a sorted array | Techie Delight
September 18, 2025 - A simple solution would be to run a linear search on the array and return the given element’s first or last occurrence. The problem with this approach is that its worst-case time complexity is O(n), where n is the size of the input. This solution also does not take advantage of the fact that the input is sorted.
🌐
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
In arrays starting index is 0 and last index is len(arr)-1 ... You can do this by performing List Slicing. 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 ...