All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Find Index Of Element In List Python

Last Updated : Jul 1, 2023

Find Index Of Element In List Python

In this tutorial we will show you the solution of find index of element in list python, while working as a programmer on data structures, there might be a requirement to find the index of some particular element in an array or a list.

It is a little bit difficult to achieve this target without using some built-in functions. So, let’s get started.

Step By Step Guide On Find Index Of Element In List Python :-

Lists in Python are an ordered collection of items and these items could be of various data types. Lists are enclosed in square brackets ([ ]) and the items in a list are separated by commas.

Index in a list starts with 0. This essentially means that the first element in the list has an index of 0 and not 1.

Finding the index of an element may seem easy when dealing with a small list; however, this could become tedious when the length of the list increases.

Python has an inbuilt function called index() to find the index of any element in a list.

animals = ['cat', 'dog', 'rabbit', 'horse']
index_animals = animals.index('dog')
print("The index of dog:", index_animals)
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
index_vowels = vowels.index('e')
print('The index of e:', index_vowels)
alphabet = ['a', 'b', 'c', 'd', 'e']
index_alphabet = alphabet.index('p')
print('The index of p:', index_alphabet)
colors = ['red', 'black', 'blue', 'green', 'pink', 'orange', 'grey', 'white']
index_colors = colors.index('pink', 4)
print('The index of pink:', index_colors)
index_colors = colors.index('grey', 3, 5)
print('The index of grey:', index_colors)
  1. In the first line there is a variable named “animals”, which is a list that contains the names of some animals.
  2. In the next line, there is another variable named index_animals, which will hold the value of the index of the “dog” in the list.
  3. In the next line there is a print statement which will display the index of “dog” in the list. This is the simplest way to find the index of any element in a list.
  4. In the next line there is a variable named vowels, which is again a list that contains some vowels in it.
  5. There is a variable in the next line which is index_vowels, which will hold the value of the index of the element “e” in the list.
  6. In the next line there is a print statement which will display the index value of the element “e”.
  7. In the line there is a variable named “alphabet” which contains some alphabetic characters.
  8. The line has an index() function that searches for an element that is not present in the list. So that statement will raise an error of “ValueError”.
  9. You have to comment the next 2 lines in order to execute the next bit of code to see the output.
  10. In the next line there is a list named “colors” which has some color elements in it.
  11. The index() function can accept 3 parameters: the first is the element you want to find the index of; the second one is the starting index in the list from which you want to start searching; and the last one is the ending index till which you want to search for an element.
  12. In the next line, the index() function has 2 parameters: “Pink” and “4”, which means that it finds the element “Pink” in the list starting from 4th element.
  13. The next line will print the index of the element “Pink” in the list.
  14. In the next line the index() function has 3 parameters: “Grey”, “3” and “5” which means search for the element “Grey” in the list starting from 3rd position to 5th position. This statement will raise an error of “ValueError” because the element “Grey” is at the 6th position.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now find the index of any element from a list in a Python program.

I hope this tutorial on find index of element in list python helps you and the steps and method mentioned above are easy to follow and implement.