Index function in Python is very helpful when we have a lot of data which can be arranged in a serial manner. In Python we can use a variety of data structures and predefined methods which can hold different types of data in an organised manner.
When you have a list of students for registration of swimming class and the third student from the top cancelled their registration, then how will you remove them from the list. This is the exact place where you will need an index function in Python. In this blog, we will learn more about how to use the index() method.
Index Function in Python Syntax & Parameters
The index function in Python consists of element, start and end parameters inside. It can be used with a list, tuple or string to search the index value of a particular element.
list.index(element, start, end) |
Here, are the parameters of the Index() function in Python.
- Element is the item you want to search for in the list. It is mandatory to declare what you are searching for in the list.
- Start is the index to start searching for the element. It is optional and you can skip this part.
- End is the last index up to which you want to search for the element. It is also optional to tell the value of end in the code.
The Index() Function in Python
The index function in Python returns the position of the data element in a given list or string. The index() method is used to return the index of the specified element in the list. Let us understand it using a simple example below.
fruits = [‘apple’, ‘banana’, ‘cherry’, ‘banana’, ‘orange’]
position = fruits.index(‘banana’) print(“The first ‘banana’ is at index:”, position) |
We need to find the element “banana” in the given list which we can easily complete using the index() method to find the position of the banana in the list.
Return Type In Index() Function In Python
In Python, list or string index() returns the index of a given element. If the value is not found in the list then you will get a valueError in the return value.
Remember that when you are using the index function in Python, then it will only return the first occurrence of the matching element.
text = “Hello, world!”
# Find the index of the first occurrence of ‘o’ index_o = text.index(‘o’) print(“The first ‘o’ is at index:”, index_o) |
You will find the ‘o’ character at the index 4 as an output on the screen. The return types are mainly integers.
What Is Index Function In Python?
The Index function in Python refers to a method of accessing the index of a specific element in a list or string. In Python, indexing starts at 0th index by default. For example, if you are accessing a string “Ankit” then the letter “A” is at 0 index, “n” is at 1 index, “k” is at 2 index, and so on.
This is a built-in function in Python which can help you find the exact position of an element in a list. If the given value which you are searching for is not present in the list, then it will return a “ValueError”. Suppose we have a list with numbers present inside i,e. [ 10, 20, 30, 40, 50, 60] and you are searching the element at list.index(7) then it will return a “ValueError”.
Examples of Index Function In Python
Let us look at some of the major examples where we can use the index() function.
1. Write a program to find the number 3 with start parameter as 2 using the index function in Python.
In this example, we have to find the index of number 3 located in the list. Also, for a twist we have been given the starting index from where the search for the index number starts. We will embed the starting index after the element we are searching in the list. We will get the index as 3 in the list.
numbers = [5, 3, 7, 3, 9, 3]
print(numbers.index(3, 2)) |
2. Write a Python program to find a message i,e. Hello World in between a given starting and ending occurrence.
In this Python program we have to find a character ‘o’ in the list from the string “hello world”. We will start searching the string from the first index i,e. 0 index. The ‘o’ character in the string is located at 4th index of the string.
message = “hello world”
print(message.index(‘o’)) |
3. Write a Python Program to find substring in a given quote using the index function in Python.
In this Python program we are looking for a word in a phrase i,e. “power” in the phrase i,e. Knowledge is power. We will start searching from the 0th index and keep on looking until we find the “power” first index. The index is the 13 number in the phrase.
quote = “Knowledge is power.”
print(quote.index(“power”)) |
4. Write a Python program to handle errors when an element is not found in a list.
In this Python program we will have a list where we can print the index of the lion and if “lion” is not present in the list then you can generate a custom valueError where you will display that “element is not found in the list”.
animals = [‘cat’, ‘dog’, ‘rabbit’]
try: print(animals.index(‘lion’)) except ValueError: print(“Element not found in the list.”) |
5. Write a program to find the index of a Dictionary with a specific value.
# List of student records
students = [ {‘id’: 1, ‘name’: ‘Alice’}, {‘id’: 2, ‘name’: ‘Bob’}, {‘id’: 3, ‘name’: ‘Charlie’}, {‘id’: 4, ‘name’: ‘Diana’} ] names = [student[‘name’] for student in students] index_of_charlie = names.index(‘Charlie’) print(“Charlie is at index:”, index_of_charlie) print(“Charlie’s full record:”, students[index_of_charlie]) |
In this example we have a dictionary with keys and values stored in a variable students. We have to find the index of a student named “Charlie”. Let us use the Index function in Python to find the index of Charlie.
![]() |
Master Python Programming & Data Structures
Become a master in Python developer with the knowledge of Data Structures in Python. Get enrolled in PW Skills Decode DSA With Python Course and get in-depth tutorials, practice exercises, real world projects and more with this self paced learning program.
You will learn through industry oriented curriculum and prepare for competitive programming with this course. Get an industry recognised certification from PW Skills after completing the course.
Learn Data Analytics With PW Skills
Master Data Analytics with PW Skills online Data Analysis program. Learn all fundamental concepts of SQL, Database, Query handling, and more.
Complete the course assessment and quizzes to download your own certificate and strengthen your portfolio with pwskills.com
Index Function in Python FAQs
Q1. What is an Index function in Python?
Ans: Index function in Python is a pre-built method used to find the index value of a specific element.
Q2. What are parameters used in Python?
Ans: There are three important parameters used in Python programming language which are element, start and end.
Q3. What is the syntax of the Index function in Python?
Ans: The index function in Python can be used as list.index(element, start, end).
Q4. Do we only get the first occurrence using the Index function in Python?
Ans: Yes, we only get the first occurrence or whichever comes first in the list or string by using the index() function in Python.