Python Tuple Access


Accessing Items In Python Tuples With Index


Python Tuples has items in it. For python tuple access or in other words, to access the items in a python tuple, we can use index numbers. The usage of this index number is inside the square brackets.


The index numbers starts with 0 and goes on. If you would like to access an item in the python tuple, you should give its index number.


  • The first item in the tuple has index 0,
  • The second item in the tuple has index 1,
  • The third item in the tuple has index 2 and so on.


You can also check How to Sort Python Tuples


So, let’s given an example tom understand python tuple access better.


cars = ("BMW", "Audi", "Mercedes")


Here, we access the second item in the tuple with item number 1. And the output is:




You can also watch the video of this lesson!




If we use negative numbers as index, this means that it will start to count from the last item. So, for negative numbers,


  • index -1 shows the last item of the python tuple,
  • index -2 shows the second last item of the python tuple and so on.



If we would like to access a range of the items in a tuple, then we use two parameters as starting item and the last item. And between them, we use colon ( : ) operator. LEt’s show this with an example.


cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")


Here, in the output we will take the third item, fourth item and the fifth item. The first one, index 2 shows third item and the last one index five shows the sixht item. And the sixth item is not included.


('Mercedes', 'Alfa Romeo', 'Aston Martin')




If we use a third operator, the step operator, then, we can also jump on some items. In the below example, we will jump one while returning with the tuple items.


cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")


('Mercedes', 'Aston Martin')




If we use only one parameter in square brackets, we can select the remainings with colon ( : ).


cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")


With this code, we take the items starting with index 3, in other words, from the fourth item of python tuple.


("Alfa Romeo", "Aston Martin", "Porche")




If we use the colon ( : ) operator in the other side;


cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")


the output will be like below. The items up to index 3 will be in the python tuple.


("BMW", "Audi", "Mercedes")




We can also use negative numbers together for python tuple access.


cars = ("BMW", "Audi", "Mercedes", "Alfa Romeo", "Aston Martin", "Porche")


The output of this code will be like below:


('Audi', 'Mercedes', 'Alfa Romeo')




Here, the negative indexes starts at the end of the tuple as we have mentined before.


And do not forget that, when we use indexes with colon ( : ) operator for python tuple access, we start with the index that is given as the first parameter and we continue to take until the second parameter. But the parameter is not included.

Back to: Python Programming Course > Python Tuples

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course