Course Content
Python Data Structures
Python Data Structures
Finding Elements in a Tuple
The index()
method in Python is used to find the first occurrence of a specified element in a tuple. It returns the index of the element, which is helpful when you need to locate the position of specific data.
The method returns the first index where the element is found.
If the element is not present in the tuple, a ValueError
is raised.
# Tuple of movies movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Find the index of "Tenet" tenet_index = movies.index("Tenet") print(f"'Tenet' is at index {tenet_index}.")
The result, 2
, indicates that "Tenet"
is the third element (since indexing starts at 0
).
Swipe to show code editor
You are given the following tuple of movie genres:
Find the index of the first occurrence of "Comedy"
in the tuple and print the result.
Thanks for your feedback!
Finding Elements in a Tuple
The index()
method in Python is used to find the first occurrence of a specified element in a tuple. It returns the index of the element, which is helpful when you need to locate the position of specific data.
The method returns the first index where the element is found.
If the element is not present in the tuple, a ValueError
is raised.
# Tuple of movies movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Find the index of "Tenet" tenet_index = movies.index("Tenet") print(f"'Tenet' is at index {tenet_index}.")
The result, 2
, indicates that "Tenet"
is the third element (since indexing starts at 0
).
Swipe to show code editor
You are given the following tuple of movie genres:
Find the index of the first occurrence of "Comedy"
in the tuple and print the result.
Thanks for your feedback!
Finding Elements in a Tuple
The index()
method in Python is used to find the first occurrence of a specified element in a tuple. It returns the index of the element, which is helpful when you need to locate the position of specific data.
The method returns the first index where the element is found.
If the element is not present in the tuple, a ValueError
is raised.
# Tuple of movies movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Find the index of "Tenet" tenet_index = movies.index("Tenet") print(f"'Tenet' is at index {tenet_index}.")
The result, 2
, indicates that "Tenet"
is the third element (since indexing starts at 0
).
Swipe to show code editor
You are given the following tuple of movie genres:
Find the index of the first occurrence of "Comedy"
in the tuple and print the result.
Thanks for your feedback!
The index()
method in Python is used to find the first occurrence of a specified element in a tuple. It returns the index of the element, which is helpful when you need to locate the position of specific data.
The method returns the first index where the element is found.
If the element is not present in the tuple, a ValueError
is raised.
# Tuple of movies movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Find the index of "Tenet" tenet_index = movies.index("Tenet") print(f"'Tenet' is at index {tenet_index}.")
The result, 2
, indicates that "Tenet"
is the third element (since indexing starts at 0
).
Swipe to show code editor
You are given the following tuple of movie genres:
Find the index of the first occurrence of "Comedy"
in the tuple and print the result.