Course Content
Python Data Structures
Python Data Structures
Accessing Elements of 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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[1]) print(countries[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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[-1]) print(countries[-4])
In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).
Task
Given the tuple:
Retrieve the first, second, fifth, and seventh items to create a new tuple:
Thanks for your feedback!
Accessing Elements of 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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[1]) print(countries[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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[-1]) print(countries[-4])
In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).
Task
Given the tuple:
Retrieve the first, second, fifth, and seventh items to create a new tuple:
Thanks for your feedback!
Accessing Elements of 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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[1]) print(countries[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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[-1]) print(countries[-4])
In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).
Task
Given the tuple:
Retrieve the first, second, fifth, and seventh items to create a new tuple:
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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[1]) print(countries[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.
countries = ('India', 'Latvia', 'Panama', 'Serbia', 'Finland', 'Germany', 'Japan') print(countries[-1]) print(countries[-4])
In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).
Task
Given the tuple:
Retrieve the first, second, fifth, and seventh items to create a new tuple: