
👉Python List index() Method with Examples
Python List index: The Python list.index() method is used to find the index of a given element in a list. This method returns the first occurrence of the specified element. In this guide, we’ll cover its syntax, parameters, and various examples to understand its functionality.
👉Python List index Syntax
python
list.index(element, start, end)
👉Python List index Parameters
- element: The element whose index you want to find.
- start (optional): Starting position to search.
- end (optional): Ending position to search.
👉Python List index Return Value
- Returns the index of the first occurrence of the element.
- Raises ValueError if the element is not found.
👉Example 1: Basic Usage of list.index()
python
my_list = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’]
print(“The index of element C is”, my_list.index(‘C’))
print(“The index of element F is”, my_list.index(‘F’))
👉Output:
pgsql
The index of element C is 2
The index of element F is 5
👉Example 2: Using start and end Parameters
python
my_list = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’]
print(“The index of element C is”, my_list.index(‘C’, 1, 5))
print(“The index of element F is”, my_list.index(‘F’, 3, 7))
print(“The index of element D is”, my_list.index(‘D’, 1))
👉Output:
pgsql
The index of element C is 2
The index of element F is 5
The index of element D is 3
👉Example 3: Handling Non-Existent Elements
python
my_list = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’]
print(“The index of element Z is”, my_list.index(‘Z’)) # Raises ValueError
👉Output:
vbnet
ValueError: ‘Z’ is not in list
👉Example 4: Finding Multiple Occurrences with for-loop
python
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
all_indexes = []
for i in range(len(my_list)):
if my_list[i] == ‘Software Moji’:
all_indexes.append(i)
print(“Indexes for element ‘Software Moji’:”, all_indexes)
Output: [0, 3, 6]
👉Example 5: Finding Multiple Occurrences with while-loop
python
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
result = []
element_index = -1
while True:
try:
element_index = my_list.index(‘Software Moji’, element_index + 1)
result.append(element_index)
except ValueError:
break
print(“Indexes for element ‘Software Moji’:”, result)
Output: [0, 3, 6]
👉Example 6: Finding Multiple Occurrences with List Comprehension
python
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
all_indexes = [i for i in range(len(my_list)) if my_list[i] == ‘Software Moji’]
print(“Indexes for element ‘Software Moji’:”, all_indexes)
Output: [0, 3, 6]
👉Example 7: Finding Multiple Occurrences with enumerate()
python
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
print(“Indexes for element ‘Software Moji’:”, [i for i, e in enumerate(my_list) if e == ‘Software Moji’])
Output: [0, 3, 6]
👉Example 8: Finding Multiple Occurrences with filter()
python
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
all_indexes = list(filter(lambda i: my_list[i] == ‘Software Moji’, range(len(my_list))))
print(“Indexes for element ‘Software Moji’:”, all_indexes)
Output: [0, 3, 6]
👉Example 9: Finding Multiple Occurrences with NumPy
python
import numpy as np
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
np_array = np.array(my_list)
item_index = np.where(np_array == ‘Software Moji’)[0]
print(“Indexes for element ‘Software Moji’:”, item_index)
Output: [0, 3, 6]
👉Example 10: Finding Multiple Occurrences with more_itertools
python
from more_itertools import locate
my_list = [‘Software Moji’, ‘Siya’, ‘Tiya’, ‘Software Moji’, ‘Daksh’, ‘Riya’, ‘Software Moji’]
print(“Indexes for element ‘Software Moji’:”, list(locate(my_list, lambda x: x == ‘Software Moji’)))
Output: [0, 3, 6]
👉Python List index Summary
- The list.index() method is a quick way to find the first occurrence of an element.
- For multiple occurrences, use alternatives like for-loop, while-loop, enumerate(), filter(), NumPy, or more_itertools.
- Each method has its own advantages in terms of performance and readability. Choose the one that best fits your coding style and requirements.