How to Find Index of Item in List Python
Python Lists are enclosed within [] brackets and contain heterogeneous items which are separated by a comma. All the items in the List in Python have an index. List Indexing starts at 0.
Python has a built-in function index () used to retrieve the lowest index in the list. We can provide conditions to get the index of a specific item in the list.
The syntax of the Python list index () is given as:
list_name.index(element, start, end)
In the parameters, an element is that item in the list to be searched. Start and End are the optional parameters for specifying starting and ending of the search.
This article will discuss different methods of finding an index of an item in a Python list. The methods will be elaborated with the help of example codes and output.
1. Index of Item in the Python List
We will take two lists of our own choice. Built-in List method index () will be used to find the index of one specific number in the python list.
Mylist1 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] item = 16 index = mylist.index(item) print('The index of this even number in the list is:', index) Mylist2 = ['lemon', 'chilli', 'potato', 'onion', 'coriander'] print(Mylist2.index('onion'))
Output:
2. Item occurring multiple times in the Python List
A list can have multiple occurrences of one item. In such a case, the index for the first occurrence will be returned.
list1 = [1, 2, 3, 4, 4, 4, 5, 1, 6, 5] print(list1.index(4, 4, 6)) print(list1.index(5, 5, 9))
Output:
3. Item that is not present in Python List
If we find an index of any such item in list that is not present inside the list, then ValueError is returned.
mylist = ['lemon', 'chilli', 'potato', 'onion', 'coriander'] item = mint index = mylist.index(item) print('The index of', item, 'in the list is:', index)
Output:
4. Index of the item using Start & End Parameters
As the start and end parameters are optional in the Python built-in list index () method, we will find an item's index using both parameters.
mylist = [2, 5, 7, 9, 11, 13, 17, 19] item = 9 start=1 end=6 index = mylist.index(item, start, end) print('The index is:', index)
Output:
5. With two arguments in the Index function
When there are two arguments in the index function, one is items in List to be searched, and the other tell from where the index search would start.
list1 = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40 ] index= list1.index(20, 3 ) print('The index is:', index)
Output:
6. Index of a Python List containing Sublist and Tuples
The following code will find an index when a Python list has sublists, a list of lists, and a list of tuples inside it.
Mylist1 = [1, 2, 3, [6, 8,10], ('mango', 'apple', 'banana')] Index1= Mylist1.index([6, 8, 10]) print('The index of sublist is:', index1) Index2= Mylist1.index(('mango', 'apple', 'banana')) print('The index of tuple is:', index2)
Output:
7. Index from List Comprehension
The concept of Python List Comprehension is used to get the indexes of the list of element occurrences in List.
Mylist1 = [10,20,30,20,40,50,20,60,10,20] res = [x for x in range(len(Mylist1)) if Mylist1 [x] == 20] print ("Indices at which 20 is present: " + str(res))
Output:
8. Index with Enumerate Function
We can get the index of a specific element in List with the help of the Python Enumerate Function.
List1 = [80, 20, 30, 20, 40, 80, 20, 60, 80, 20] res = [x for x, z in enumerate(List1) if z == 80] print ("Indices at which 80 is present: " + str(res))
Output:
CONCLUSION
This article has discussed distinct methods to find the index of an item in Python List. I hope the example codes are helpful for the proper understanding of each technique.
How to Find Index of Item in List Python
Source: https://www.codeleaks.io/how-to-find-an-index-of-an-item-in-python-list/
0 Response to "How to Find Index of Item in List Python"
Post a Comment