Methods and Functions

Python List index()

Python lists are collections of objects that are ordered. That is, the entries in the list are ordered, and each element may be referenced to using an index. An index is a number that specifies the position of a given element in a list. Items in a list are indexed using zero-based indexing, which means they are indexed beginning with zero. To find out the index of each element in a list, we use the Python list index() function. The Python list index() method returns the index value of an element indicating the position of that element in the list.

Python list index

Definition

  • Python list index() function is used to return the index value of the specified element in the list.
  • The Python list index() is a built-in function that searches for a specified element from the beginning of the list and returns the lowest index at which the element appears.

Python list index()

The first element is at index zero, the second at index one, and so on. If a list has N elements, the last one is at index N – 1.

Syntax of List index()

The syntax followed by Python’s list index() function is:

                    

list_name.index(element, start, end)

List index() Parameters

The list index() method accepts a maximum of 3 parameters. They are:

  • element – the element in the list to be searched
  • start (Optional) – begins searching the element from this index value
  • end (Optional) – searches the element up to this index value

Note – If no element is specified in the parameter, the index() function returns a TypeError exception.

Return value from List index()

  • The Python list index() method returns the index of the specified element if it exists in the list.
  • If the element is not found in the list, a ValueError exception is raised.

Note – If the index() method finds multiple occurrences of the matching element in the list, it will return the index value of the first occurrence of that element.

Example 1: Find the index of the element

Example

                    

# Python program to illustrate list index()
# num list
num = [1, 4, 8, 2, 8, 3, 9, 6]
# index of 8 in num
index = num.index(8)
print('Index of element 8:', index)

# index of first Python is returned
lang = ['Java', 'C', 'C++', 'Python', 'Python']
index = lang.index('Python')
print('First occurance of element Python:', index)

Output

                    

Index of element 8: 2
First occurrence of element Python: 3

Example 2: Index of the Element not Present in the List

If the value whose index is to be returned is not found, the Python interpreter throws a ValueError.

Example

                    

# Python program to illustrate list index()
vowels = ['a', 'e', 'i', 'o', 'u']

# index of 's' in vowels
index = vowels.index('s')
print('Index value of s is:', index)

Output

                    

Traceback (most recent call last):
File "", line 5, in 
ValueError: 's' is not in list

Example 3: Working of index() With Start and End Parameters

Example

                    

# Python program to illustrate list index()
animals = ['cat', 'dog', 'rat', 'lion', 'deer', 'dog', 'bear']

# search index of dog element
index = animals.index('dog')
print('The index of dog:', index)

# search index of dog element by using start and end parameter
index = animals.index('dog', 3, 7)
print('The index of dog:', index)
print('')

lang = ['Java', 'C', 'C++', 'Python', 'R', 'SQL', 'Python']
# using only element parameter
index = lang.index('Python')
print('First occurrence of element Python:', index)

# using start parameter
next_pos = lang.index('Python', index+1)
print('Next occurrence of element Python:', next_pos)

Output

                    

The index of dog: 1
The index of dog: 5

First occurrence of element Python: 3
Next occurrence of element Python: 6

Frequently Asked Questions

Q1. Can you index a list in Python?

Python lists are collections of objects that are ordered. That is, the entries in the list are ordered, and each element may be referenced to using an index. To find out the index of each element in a list, we use the Python list index() function. The Python list index() method returns the index value of an element indicating the position of that element in the list.

To index a list in Python we follow the below syntax:

                    

list_name.index(element, start, end)

Example

                    

city = ['Kolkata', 'Panaji', 'Mumbai', 'Chennai', 'Delhi', 'Mumbai']

pos = city.index('Mumbai')
print('Index value of element Mumbai is:', pos)

Output

                    

Index value of element Mumbai is: 2

Q2. How do I see all indexes of an element in Python?

To find out all the occurrences of an element in the list, we have few different methods in Python. Some of them are:

  1. Using enumerate() function – You may use the built-in function enumerate to find the index of all instances of an element in a list ().

Example

                    

num = [1, 2, 7, 6, 4, 2, 9, 2]
val = 2

for i, j in enumerate(num):
    if j == val:
        print('The index value of', j, 'is:', i)

Output

                    

The index value of 2 is: 1
The index value of 2 is: 5
The index value of 2 is: 7

  1. Using range() function – You may also use the range() function to retrieve a list of all valid indices and then match the matching value at each index with the specified item.

Example

                    

num = [1, 2, 7, 6, 4, 2, 9, 2]
val = 2

for i in range(len(num)):
    if num[i] == val:
        print('The index value of', num[i], 'is:', i)

Output

                    

The index value of 2 is: 1
The index value of 2 is: 5
The index value of 2 is: 7

Q3. Is Python 0 or 1 indexed?

In the Python programming language, the indexing for any iterable starts from 0. So, the index value of the 1st element is 0, the 2nd element is 1, and so on.

Example

                    

num = [10, 20, 30, 40]

for i in num:
    print('Index of', i, 'is:', num.index(i))

Output

                    

Index of 10 is: 0
Index of 20 is: 1
Index of 30 is: 2
Index of 40 is: 3

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.