In this Python post, we’ll learn how to create Python tuple and fetch data from it. We will be using multiple practical examples to properly demonstrate how Python tuple works.
After reading this post, I am sure you’ll be able to easily incorporate Python tuple in your own code. So let’s not keep you waiting anymore and get right into its implementation.
What is Python Tuple?
It stores a list of items, which means its like a Python list. It is not mutable means that we cannot change the items in tuple once they are assigned. Whereas in Python list, we can easily change/replace existing items.
We will now practically learn how to create Python tuple with multiple ways and after that, we’ll go through multiple ways on how to get/fetch items from it.
Creating a Python Tuple
We can make use of the parenthesis( ( ) ), it works as a body of tuple. We can assign items, separated by commas. We can also define Python tuple without parenthesis which we will see in the next section.
tupleName = (item1, item2, item 3)
The parenthesis are not necessary, you can even define a tuple without them. See below:
tupleName = item1, item2, item 3
Minimum of two items are necessary to create tuple. See below:
tupleItems=34 print(type(tupleItems)) // type is int tupleItems=(34) print(type(tupleItems)) // type is int
What you can do here is that you can use the comma( , ) after this item which will notify Python that it is actually a tuple. See below code for practical demonstration:
tupleItems=34, print(type(tupleItems)) // type is tuple tupleItems=(34,) print(type(tupleItems)) // type is tuple
Datatypes In Python Tuple
The tuple accept single as well as multiple datatypes. See below:
tupleItems=(34,45,56,24) //single datatype tuple tupleItems=(34,45,56,'green',24,3.5) //multiple datatype tuple tupleItems=(34,45,56,24) //single datatype type tupleItems=(34,45,56,24,('green',33.4,21), [2,4,3]) //nested tuple
Unpacking a Tuple
Unpacking is assigning each value of tuple to some variable. See below code:
item1,item2,item3=(34,65,'red') print(item1) print(item2) print(item3)
First item will be assigned to the first variable and so on. Just make sure that the number of variables matches the number of items in tuple. You can assign any name to the variables.
Fetch Data from Python Tuple
To access items from the tuple, we have use the index operator . For nested tuples, we have to use nested index operator  like this.
Index value starts from 0 and ends at size of tuple -1. For instance, a tuple of 5 items will have index range from 0 to 4. See below examples:
tupleItems=(34, 'brown' ,45 ,56 ,24 ,('green', 33.4, 21),['white', 'purple']) print( tupleItems ) // 34 print( tupleItems ) // shows w from the brown string print( tupleItems ) // 33.4 print( tupleItems ) // white
Negative Indexing In Python Tuple
It means getting data using the index but negative means it will start from the last item. See below code:
tupleItems=(21, 53, 5.6, 3.87, 'green', 'blue', 34) print( tupleItems[-1] ) // 34 print( tupleItems[-2] ) // blue print( tupleItems[-7] ) // 21
As you can see, last item of tuple will have a -1 index, second last will have -2 and so on. It means a tuple of 5 items will have negative index range of -1 to -5.
Using slicing, we can specify the range of items to get from the Python tuple. We have to use the colon ( : ) to apply slicing to tuple. See below code:
tupleItems=(21, 53, 5.6, 3.87, 'green', 'blue', 34) print( tupleItems[2:5] ) // (5.6, 3.87, 'green') print( tupleItems[:] ) // (21,53,5.6,3.87,'green','blue',34) print( tupleItems[:5] ) // (21, 53, 5.6, 3.87, 'green') print( tupleItems[:-5] ) // (21, 53)
As you can see here that we have specified a range from the 3 item to the 6 item of tuple but slicing apply -1 to the ending range index like this: tupleItems[2 : 5-1].
- In the second example, you can see that if you don’t define any range then it will take all the items of the tuple.
- Third example shows that not specifying the beginning of range will take all the items of before the ending range index.
- Negative slicing is also possible, we have specified the ending range which is -5 starting from right so it will print all the items of the left side as no starting range is specified.
Changing a Python Tuple
As you already know, tuple is immutable, means its item cannot be changed. But if you have a mutable nested inside tuple, then you can change its items. See below:
tupleItems=(21,53,[5.6,3.87],'green',34) tupleItems=294 //TypeError: 'tuple' object does not support item assignment tupleItems=22 // (21, 53, [5.6, 22], 'green', 34) it works just fine
Reassigning Values To Tuple
You can actually reassign values to the existing tuple. See below code:
tupleItems=(21, 53, [5.6,3.87], 'green', 34) print( tupleItems ) // (21, 53, [5.6, 3.87], 'green', 34) tupleItems=(21, 'white', 'green', 34, [5.6, 22]) print( tupleItems ) // (21, 'white', 'green', 34, [5.6, 22])
Adding two or more tuples can be done through the + operator. See below code:
print( ( 1, 4, 5 ) + ( 9, 6, 3 ) ) // (1, 4, 5, 9, 6, 3)
Repeating Tuple Items
If you want the items of tuple to be shown multiple times then use the * operator. See below code:
print( (1, 4, 5 ) * 3 ) // (1, 4, 5, 1, 4, 5, 1, 4, 5)
Deletion of Tuple
We cannot delete a specific item from Python tuple, but we can delete an entire tuple. See below code:
tupleItems=(1,4,3) // a simple tuple having some items so we can test the deletion del tupleItems // TypeError: 'tuple' object doesn't support item deletion del tupleItems // delete the entire tuple print(tupleItems) // NameError: name 'tupleItems' is not defined. It means the tuple is deleted successfully.
So this is how you can create, customize, fetch and delete Python tuple. Don’t hesitate to ask if you have any questions related to the implementation of Python tuples. I’d be very happy to answer all.
To conclude, hope you now have a complete practical know ledge of how to implement and customize tuples in Python. I’d be happy to have your valuable feedback on this Python post. Thank you for reading.