Python index List Function

Python index List is one of the List functions used to find the index of an item from a given list. In this section, we discuss how to use this Python List index function with practical examples.

The syntax of the list index function is

list.index(list_item)

Python index List Function Example

The list index function finds the index of an item from a given List. The below code finds the index position of 10 and 50 from an integer list.

# Python index List Function

a = [20, 30, 10, 40, 10, 50, 20]

print("The Index of 10 = ", a.index(10))
print("The Index of 50 = ", a.index(50))

List index function output

The Index of 10 =  2
The Index of 50 =  5

index List Example 2

In this List functions example, we declared a string List. Next, we used the index function to find the index position of Orange and Kiwi.

# Python index List Function

Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape']

print("The Index  Position of 'Orange' = ", Fruits.index('Orange'))
print("The Index Position of 'Kiwi' = ", Fruits.index('Kiwi'))

List index function output

The Index  Position of 'Orange' =  1
The Index Position of 'Kiwi' =  3

index List Function Example 3

This list index program is the same as the first example. However, this time we are allowing the user to enter the length of a List. Next, we used For Loop to append those numbers to the Python list.

# Python index List Function

intIndexList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intIndexList.append(value)
    
item = int(input("Please enter the Item that you want to Find: "))

print("The Index  Position of Given Item = ", intIndexList.index(item))
Python Index List Function 3

List index Function Example 4

This program allows users to enter their own string or words and then finds the index position of a specified word.

# Python index List Function

strIndexList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = input("Please enter the Value of %d Element : " %i)
    strIndexList.append(value)
    
item = input("Please enter the Item that you want to Find: ")

print("The Index  Position of Given Item = ", strIndexList.index(item))

List index function output.

Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : Apple
Please enter the Value of 2 Element : Kiwi
Please enter the Value of 3 Element : Banana
Please enter the Value of 4 Element : Orange
Please enter the Item that you want to Find: Banana
The Index  Position of Given Item =  2