Note
Python - index() - Find the position of an element
Overview of the Python index() Function: Find the Position of an Element in Ordered Collections
collection.index(value, start=None, end=None) is a built-in method that returns an index of a provided element in the data structure that
it was called on.
It can be called only on ordered collections like list, tuple, and string.
Method index() also accepts two optional index argument between which it will be looking for a given value.
The method returns the index of the first occurrence of the specified element.
We need to remember that if the element is not in a given collection we will receive ValueError!
Parameters:
-
value: The element whose index you want to find. -
start(optional): The starting index to search from. -
end(optional): The ending index (exclusive) for the search.
Basic Usage with Lists, Tuples, and Strings
few_letters_list = ["a", "b", "c"]
index = few_letters_list.index("b")
print(index) # 1
few_numbers_tuple = (1, 5, 2, 3, 4, 4, 5, 3, 3, 3)
index = few_numbers_tuple.index(3)
print(index) # 3
my_string = "DevCuriosity"
index = my_string.index("C")
print(index) # 3
Output:
1
3
3
Please notice that in few_numbers_tuple we had more than one value 3 but the method returned an index
of the first value 3 in our tuple.
Using index() with a start Argument
And we can also provide optional arguments with indexes between which we would like to search for our value.
few_letters_list = ["a", "b", "c", "a"]
index = few_letters_list.index("a", 2)
print(index) # 3
Output:
3
In this example we provided only one optional index argument, which told Python to look for value "a"
starting from index 2. Because of that, we got the index number of the second letter "a" - 3.
Using index() with a start and end Argument
Here, we provided both optional arguments and their value made the letter "a" impossible to find which
resulted in ValueError:
few_letters_list = ["a", "b", "c"]
index = few_letters_list.index("a", 1, 2)
print(index) # ValueError: 'a' is not in list
Output:
ValueError: 'a' is not in list
Key Points to Remember about the index() Function
-
First Match Only: The method always returns the index of the first occurrence of the value.
-
Works on Ordered Collections: Only works with lists, tuples, and strings. Cannot be used with unordered types like sets or dictionaries.
-
Optional Range: The start and end arguments let you limit the search to a specific range.
-
Raises ValueError: If the value is not found in the specified range or the entire collection, it raises a
ValueError.
I hope you found this helpful! If you have any feedback, questions or requests, please reach out to me through Contact page!
You might also be interested in:
- 💡 Python - type() - Determine the type of an object
- 💡 Python - count() - Count occurrences of an element in a collection
- 💡 Python - round() - Round numbers to specified decimal places
- 💡 Python - partial() - Simplify function arguments with functools
- 💡 Python - all() - Check truthiness of all elements in an iterable