How To Find Index Of Value In List Python
Python – Find Index or Position of Element in a List
To find index of the starting time occurrence of an element in a given Python List, yous can utilize index() method of List class with the element passed as argument.
alphabetize = mylist.alphabetize(element)
The alphabetize() method returns an integer that represents the index of first match of specified element in the Listing.
You can also provide beginning and terminate positions of the List, where the search has to happen in the list.
Following is the syntax of index() function with kickoff and end positions.
index = mylist.index(x, [start[,end]])
start
parameter is optional. If y'all provide a value for start
, then end
is optional.
We shall expect into examples, where we go through each of these scenarios in detail.
Case 1: Discover Index of item in List
In the following example, nosotros have taken a List with numbers. Using index() method we will find the index of item eight
in the list.
Python Programme
mylist = [21, v, 8, 52, 21, 87] item = eight #search for the particular alphabetize = mylist.index(item) print('The index of', item, 'in the listing is:', index)
Run
Output
The index of 8 in the list is: ii
The element is present at 3rd position, so mylist.alphabetize()
role returned ii.
Instance ii: Observe Index of Detail in Listing – beginning, end
In the following example, nosotros take taken a Listing with numbers. Using alphabetize() method nosotros will detect the index of item 8
in the list. Besides, nosotros shall laissez passer offset and end. index() office considers only those elements in the list starting from start
index, till stop
position in mylist
.
Python Programme
mylist = [21, 8, 67, 52, 8, 21, 87] particular = eight showtime=2 stop=vii #search for the item index = mylist.alphabetize(detail, start, finish) impress('The index of', item, 'in the list is:', alphabetize)
Run
Output
The index of 8 in the list is: four
Caption
mylist = [21, viii, 67, 52, 8, 21, 87] ----------------- merely this part of the list is considered ^ index finds the element hither 0 1 2 3 4 => iv is returned by index()
Instance 3: Notice Index of Item – Particular has multiple occurrences in List
A Python Listing can contain multiple occurrences of an element. In such cases, only the index of first occurrence of specified element in the listing is returned.
Python Program
mylist = [21, five, 8, 52, 21, 87, 52] item = 52 #search for the item alphabetize = mylist.alphabetize(item) print('The alphabetize of', item, 'in the listing is:', index)
Run
Output
The index of 52 in the list is: 3
The element 52
is nowadays two times, but only the index of first occurrence is returned by index() method.
Permit united states empathise how index() method works. The function scans the listing from starting. When the item matches the argument, the function returns that index. The later occurrences are ignored.
Instance 4: Find Index of Particular in List – Item not present
If the element that nosotros are searching in the List is non nowadays, you will go a ValueError
with the message item is not in list
.
In the following program, we have taken a list and shall attempt to find the index of an element that is non present in the listing.
Python Program
mylist = [21, v, 8, 52, 21, 87, 52] item = 67 #search for the detail/element index = mylist.index(item) impress('The alphabetize of', particular, 'in the list is:', alphabetize)
Run
Output
Traceback (most recent call last): File "instance.py", line 5, in <module> alphabetize = mylist.index(item) ValueError: 67 is non in list
As alphabetize() tin can throw ValueError, use Python Try-Except while using index(). In the following example, we shall learn how to utilise try-except statement to handle this ValueError.
Python Program
mylist = [21, 5, 8, 52, 21, 87, 52] item = 67 try: #search for the detail index = mylist.index(particular) impress('The index of', item, 'in the listing is:', index) except ValueError: impress('item not present')
Run
Output
particular non present
The item, whose index nosotros are trying to notice, is not present in the list. Therefore, mylist.index(detail)
throws ValueError. except ValueError:
block catches this Mistake and the corresponding block is executed.
Summary
In this Python Tutorial, we learned how to detect the index of an element/particular in a listing, with the help of well detailed examples.
Source: https://pythonexamples.org/python-find-index-of-item-in-list/
Posted by: newtondictiony.blogspot.com
0 Response to "How To Find Index Of Value In List Python"
Post a Comment