This tutorial helps to understand Python tuple with examples. It’s a very common and useful collection types. A tuple is a collection that is ordered and unchangeable.
How To Define Tuple in Python
Python tuples are written with round brackets.
tuple_item = ('Red', 'Green', 'Yellow') print(tuple_item)
Access Tuple Items
We can access tuple items by referring to the index number of the tuple item, inside square brackets:
tuple_item = ('Red', 'Green', 'Yellow') print(tuple_item[1]) # Green
Negative Indexing
You can also use negative indexing with a tuple. The negative index beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
tuple_items = ('Red', 'Green', 'Yellow') print(tuple_item[-1]) # Yellow print(tuple_items[1])
The Above code will print the second item of the tuple.
Negative Indexing Into Python Tuple
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
tuple_items = ('Red', 'Green', 'Yellow') print(tuple_items[-1])
The Above code will print the last item of the tuple.
Slicing of Python Tuple
We can specify a range of indexes by specifying where to start and where to end the range. The return value will be a new tuple with the specified items.
We can also pass a negative index to a slice of the tuple.
tuple_items = ("Red", "Green", "Yellow", "Orange", "Pink") print(tuple_items[1:3]) print(tuple_items[-4:-1])
The negative range of indexes will generate a new tuple that has index -4 (included) to index -1 (excluded).
Output:
('Green', 'Yellow') ('Green', 'Yellow', 'Orange')
Change Item Value
As We know, the Tuples are unchangeable, or immutable So we can not change it. But there is a workaround. We can convert the tuple into a list, change the list, and convert the list back into a tuple.
tuple_items = ("Red", "Green", "Yellow", "Orange", "Pink") list_items = list(tuple_items) list_items[1] = "Green1" tuple_items = tuple(list_items) print(tuple_items)
Output:
('Red', 'Green1', 'Yellow', 'Orange', 'Pink')
Loop Through a Tuple Item
You can loop through the Tuples items by using a for
loop:
tuple_items = ("Red", "Green", "Yellow", "Orange", "Pink") for x in tuple_items: print(x)
The above code will print all items of tuple:
Red Green Yellow Orange Pink
Check if Item Exists
To determine if a specified item is present in a tuple use the in
keyword:
tuple_items = ("Red", "Green", "Yellow", "Orange", "Pink") if "Green" in tuple_items: print("Yes, 'Green' is in the tuple")
The above code will check if “Green” is present in the tuple.
How To Count Length
The len()
method is used to determine the length of the tuple.
tuple_items = ("Red", "Green", "Yellow", "Orange", "Pink") print(len(tuple_items)) #5
Add Items Into The Tuple
You can not add an item to the tuple.
How to Remove An Item From the Python Tuple
Tuples are unchangeable and immutable, so you cannot remove items from it, but you can delete the tuple completely
tuple_items = ("Red", "Green", "Yellow", "Orange", "Pink") del(tuple_items)
How To Join Two Tuple
We can join two or more tuples using +
operator.
tuple_items = ("Red", "Green", "Yellow") tuple_items1 = ("Danger", "Success", "Warning") combined_list = tuple_items + tuple_items1 print(combined_list)
Output:
('Red', 'Green', 'Yellow', 'Danger', 'Success', 'Warning')