Python Program to Get the index of an item in List – 3 Easy ways

List:

A list in Python is a collection of zero or more elements. A list element can be any type of data. It can be a string, a number, or a mixture of the two. In Python, a list is equivalent to an array in C or Java. Lists are mutable, which means you may change their content, and they contain a variety of helpful specialized methods.

A Python list is a data structure that efficiently performs the function of an Array. Furthermore, unlike arrays, a “list” keeps elements in a dynamic manner and can be used to store elements of different types.

As a result, Lists are a better substitute for the Array data structure because they may hold a variety of elements(heterogeneous elements).

Program to Get the index of an Item in List – 3 Easy ways

1)Using List Comprehension

Python List Comprehension can be used to get a list of indices of all the occurrences of a specific element in a List.

Syntax:

[expression for element in iterator if condition]

We may obtain the index values, i.e. the position of all occurrences of an item in the List, using List comprehension.

Example

# Give the list as static input and store it in a variable.
gvn_lst = [5, 2, 4, 10, 5, 6, 5, 8]
# Print the given list
print("The given list = ", gvn_lst)
# Check if the element present at the iterator index of the given list is equal to
# 10 using the if conditional statement and list Comprehension
# Store it in a variable.
rslt = [i for i in range(len(gvn_lst)) if gvn_lst[i] == 5]
# Print The all index values of  5 in the given list
print("The all index values of 5 in the given list =", rslt)

Output:

The given list =  [5, 2, 4, 10, 5, 6, 5, 8]
The all index values of 5 in the given list = [0, 4, 6]

2)Using index() Method

The index() function in Python can be used to retrieve the index value of a specific List element.

Syntax:

index(element, start, end)

Parameters

element: The element for which the index value must be retrieved.

start: This is optional. It is the start value from which the search begins.

end: This is optional. It is the end value where the search ends.

In contrast to other methods, the index() method only returns the index value of the first occurrence of the specified item in the list.

If the specified element does not exist in the List, a ValueError exception is thrown.

Example

# Give the list as static input and store it in a variable.
gvn_lst = [5, 2, 4, 10, 5, 6, 5, 8]
# Pass some random number as an argument to the index() method to get the first occurrence
# of index value of given element.
# Store it in another variable.
rslt = gvn_lst.index(5)
# Print the index value of given element in the given list.
print("The first occurrence of index value of 5 in the given list =", rslt)

Output:

The first occurrence of index value of 5 in the given list = 0

3)Using enumerate() Method

The Python enumerate() function can also be used to return the index positions of all occurrences of a specific element in a List.

Example

 

In this case, the enumerate() method creates a counter that increments after each successful search of that particular item and provides its index value.

Leave a Comment