How to Get Index or Position of Item in List?

The list data type in Python is used to keep values of more than one data type. It takes the values in the square bracket using comma-separated syntax. Every item or element in the list has a position or index. To get the value from the list, we need to access the index of that particular value. Various methods are used in Python to fetch the position of items in the list.

This post will enlist various approaches that help you to find the item’s index in the list using the following approaches:

Method 1: Using list_value.index() function

The “list.index()” function is used to get the position of specific items in Python. Let’s understand it by the following examples code:

Example 1: Get Index of Items in the List Using “list.index()” Function

In the below code the “list.index()‘ function is used to get the index of the specific items in the list. Here is an example:

Code:

list_value = ['Alex', 18, 'John', 22]
output = list_value.index('John')
print('Index of Item is:', output)

The above example states that the “list.index()” function is used to accept the list variable “list_value” as an argument and return the index.

Output:

The above output shows that the specified item “John” index is  “2”.

Note: If an item exists at multiple indexes in a list, the “list.index()” function returns its first index value.

Example 2: Get Index of Items in the List at Specific Range

The “list.index()‘ function is used to fetch the index of the specific items in the list at a user-defined index range.  Here is an example:

Code:

list_value = ['Alex', 18, 'John', 22, 'Lily', 19]
output = list_value.index('Lily', 2, 5)
print('Index of Item is:', output)

In the above code, the “list.index()” accepts three parameters. The first parameter accepts an item value “Lily”. The second and third parameters of “list.index()” accept the starting and ending ranges “2” and “5”.

Output:

The above output shows the index of the specific item “Lily” from a specific range (2, 5).

Method 2: Get Index of All Occurrences of a Specific Item

To get the index position of all the items in the given “list,” the “for” loop and “append()” functions are used in Python. Here is an example code:

Example 1: Using for Loop and range() Function

In the below code, the “for” loop is used with the “range()” function to get the index of all occurrences of a specific item:

Code:

name_list = ['Alex', 18, 'John', 22, 'Lily', 18]
empty_list = []
for value in range(len(name_list)):
    if name_list[value] == 18:
      empty_list.append(value)
print(empty_list)

In the above code, the “for” loop performs iteration on the given range() of the list that is found using the len() function. For each iteration, the “if” statement checks the specific item value in the list. If the value is matched, the “append()” function appends the value into a new list.

Output:

The above output shows that the specific item “18” is placed at index “1” and index “5”.

Example 2: Using for Loop and enumerate() Function

In the below code, the “for” loop is used with the “enumerate()” function to get the index of all occurrences of a specific item:

Code:

name_list = ['Alex', 18, 'John', 22, 'Lily', 18]
print([i for (i, item) in enumerate(name_list) if item == 18])

In the above code the “enumerate()” function is used to store the indices of the items that satisfy the “if” condition. The for loop iterates on enumerate function and prints the index or position of the targeted item in a list.

Output:

The above output shows that the specific item “18” is placed at index “1” and index “5”.

Conclusion

The “list.index()” function is used to get the index of items from the complete list or a range of indexes in the list. The “for” loop is used to find the index of multiple occurrences of a specific item in the list. This article presented various methods to get the index of the specific item in the given list.