Answer: To find the index of an element from the given list, we can use the index() function.
The index is used to access or modify elements in the collection the first element in a list has an index of 0, the second element has an index of 1, and so on. Here’s how we can use it, along with simple examples.
Table of Contents:
Methods to Find the Index for a Given Item in a List in Python
There are 3 effective and readable methods to find the index for a given item in a list:
Method 1: Using the index() Method to Find the Index of an Item in a List
The index() method is used to access or modify elements in the collection, such as a list, array, or string. The first element in a list has an index of 0. The second element has an index of 1, and so on.
Syntax:
list_or_string_name.index(element, start_pos, end_pos)
Here, the lowest index in a list/string is called ‘element,’ and the position where the search begins is ‘start_pos’, by default, it is 0, and the position where the search ends is called ‘end_pos’.
Example 1: Using the start_pos parameter
Output:
The code searches for the first occurrence of ‘apple’ starting from index 1. It finds the second occurrence of ‘apple’ index 3 in the list
Example 2: Using both start_pos and end_pos parameters
Output :
The code searches for ‘apple’ between index 1 and 4 in the list. It finds the second occurrence of ‘apple’ at index 3.
Note: If the index is found, it returns the first index where it appears. If not, it raises a ValueError.
Method 2: Using the enumerate() for Finding the Index in the List using Python.
The enumerate() function is used to search for multiple occurrences, and if we want both the index and value, we can use the enumerate() function, which returns the enumerate object(iterators with index and the value)
Example 1: With default starting position
Output:
The code uses the enumerate method to start indexing from 0 and loops through the list of the ‘fruits’. It prints the index and corresponding fruit, starting from index 0.
Example 2: Starting Index from a Different Number
We can specify the start parameter to start the index from any number
Output:
The code uses the enumerate method to start indexing form 2 and loops through the list of the ‘fruits’. It prints the index and corresponding fruit, starting from index 2.
Method 3: Using a Loop for Finding the Index in the List using Python
A for loop is typically used to iterate through a list, checking each element one by one. For example, you might want to search case-insensitively and find multiple occurrences. We can use the loop function.
Example:
Output:
The code searches for item 30 in the list and stores its index if found. By using an if-else statement, it prints the index if the item is found. Otherwise, it indicates the item is not found in the list.
Using Index() for Strings in Python
The index( ) method in Python can find the index of a substring within a string. It operates similarly to the list index( ) method, but this method is meant specifically for strings.
Example:
Output:
The code searches for the occurrences of ‘p’ in the string. It finds the occurrence of ‘p’ at index 25 and prints it.
Syntax:
string.index(substring, start_pos, end_pos)
Here, the substring is a sequence of characters you’re trying to locate within a larger string that specifies the part of the string. “start_pos” starts the search, and “end_pos” ends the search.
Example 1: Using start_pos to start searching from a specific position:
Output:
The code searches for the first occurrences of ‘i’ starting from index 5 in the string. It finds the second occurrence of i’ at index 18 and prints it.
Example 2: Using start_pos and end_pos:
Output:
The code searches for the first occurrences of ‘i’ between 13 and 20 in the string. It finds the second occurrence of i’ at index 18 and prints it.
Conclusion
In Python, the index() method finds the index of a specified element in a list or string and returns its position. It returns the first occurrence index, which starts with 0. Otherwise, it raises a ValueError if the element is not found. Alternative methods like enumerate() or loops can also be used to find the index of an item. Understanding these approaches allows you to effectively manage the list.