python
Access items of tuple in Python
We can use the methods explained in this post to access the items of a tuple created in python.
names_tuple = ("Rick", "John", "Gavin", "Troy")
# Access second item
result = names_tuple[2]
print(result)
Output
Gavin
Negative index - start from the end
We can also use negative indexing to access the tuple items. This will start the search from the end.
Code Example - Get the last item from the list
names_tuple = ("Rick", "John", "Gavin", "Troy")
result = names_tuple[-1]
print(result)
Output
Troy
Code Example - Access the second last item from the list
names_tuple = ("Rick", "John", "Gavin", "Troy")
result = names_tuple[-2]
print(result)
Output
Gavin
Was this helpful?
Similar Posts