Course Content
Python Data Structures
Python Data Structures
Accessing Elements in a Tuple
Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0
, just like with lists.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the second movie print(movies[1]) # Accessing the sixth movie print(movies[5])
Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1
, the second to last is -2
, and so on.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the last movie print(movies[-1]) # Accessing the fourth movie from the end print(movies[-4])
In the example above, we access the last element (indexed at -1
) and the fourth element from the end (indexed at -4
).
Swipe to show code editor
Access and print the title and release year of the movie from the movie_details
tuple. Use indexing to retrieve these values.
Thanks for your feedback!
Accessing Elements in a Tuple
Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0
, just like with lists.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the second movie print(movies[1]) # Accessing the sixth movie print(movies[5])
Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1
, the second to last is -2
, and so on.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the last movie print(movies[-1]) # Accessing the fourth movie from the end print(movies[-4])
In the example above, we access the last element (indexed at -1
) and the fourth element from the end (indexed at -4
).
Swipe to show code editor
Access and print the title and release year of the movie from the movie_details
tuple. Use indexing to retrieve these values.
Thanks for your feedback!
Accessing Elements in a Tuple
Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0
, just like with lists.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the second movie print(movies[1]) # Accessing the sixth movie print(movies[5])
Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1
, the second to last is -2
, and so on.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the last movie print(movies[-1]) # Accessing the fourth movie from the end print(movies[-4])
In the example above, we access the last element (indexed at -1
) and the fourth element from the end (indexed at -4
).
Swipe to show code editor
Access and print the title and release year of the movie from the movie_details
tuple. Use indexing to retrieve these values.
Thanks for your feedback!
Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0
, just like with lists.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the second movie print(movies[1]) # Accessing the sixth movie print(movies[5])
Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1
, the second to last is -2
, and so on.
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the last movie print(movies[-1]) # Accessing the fourth movie from the end print(movies[-4])
In the example above, we access the last element (indexed at -1
) and the fourth element from the end (indexed at -4
).
Swipe to show code editor
Access and print the title and release year of the movie from the movie_details
tuple. Use indexing to retrieve these values.