Python tuple

Tuple is a collection which is ordered and and unchangeable. Tuple is created using round bracket ( () ) or by separating item with a comma( , ) . Tuple items can be any item such as integer, string, dictionary, set, tuple etc. 

Creating a tuple

Tuple is creating using round brackets ( ()  ) or using comma for multiple items. Let’s look at some of the example of dictionary

Example:

t1 = ()  # empty tuple
t2 = "a",  # also tuple
t3 = ("a", "b", "c")  # tuple
t4 = "a", "b", "c"  # tuple

Other example of tuple using mixed data type

mixed_tuple = (1, 2.5, 'first_name', [3, 4, 5], {1, 2, 3}, {'one': 1, 'two': 2})

Selecting an item from a tuple

You can select item from a tuple using indexing with index value.

Example

my_tuple = ('First Name', 'Last Name', 'Roll Number', 'Score')
print(my_tuple[0])  # first element
print(my_tuple[-1])  # last element

Output:

First Name
Score

Updating and adding item in a tuple

Adding and updating item in the tuple is not supported. However we can convert it to a list and then change the item and convert back to a tuple again.

Example:

my_tuple = ('First Name', 'Last Name', 'Roll Number', 'Score')
list_item = list(my_tuple)
list_item[0]= 'Email'

my_tuple = tuple(list_item)
print(my_tuple)

Output:

('Email', 'Last Name', 'Roll Number', 'Score')

Deleting a tuple

Items can not be deleted in a tuple. However, a tuple itself can be removed. You can use del keyword to delete entire tuple

Let’s see an example and understand this concept better.

my_tuple = ('First Name', 'Last Name', 'Roll Number', 'Score')
del my_tuple

Now the ‘my_tuple’ tuple does not exist as it has been deleted

Looping through item in a tuple

Looping through item in tuple is simple. You can loop through items like list.  Lets see an example.

my_tuple = ('First Name', 'Last Name', 'Roll Number')
for item in my_tuple:
    print(item)

Output:

First Name
Last Name
Roll Number

Creating tuple with single item 

When you create a tuple with single item you should be careful to give comma ( , ) at the end of a tuple. If you leave this comma it won’t be tuple. Let’s see an example:

# This is a single item tuple
t1 = ('one',)
print(type(t1))

t2 = 'One',  # note the ending comma, it makes it tuple
print(type(t2))

# This is not a tuple it's a string as it has no comma inside
t3 = ('one')
print(type(t3))
# it's a string
t4 = 'one'
print(type(t4))

Output:

<class 'tuple'>
<class 'tuple'>
<class 'str'>
<class 'str'>

Length of a tuple

Length of a tuple can be determined using len(iterable_item)  method.

Example,

my_tuple = ('First Name', 'Last Name', 'Roll Number')
print(len(my_tuple))

Output:

3

Checking item in a tuple

You can check if an item exist or not in a tuple using  in  keyword. You don’t have to loop through tuple with if condition. For example

my_tuple = ('First Name', 'Last Name', 'Roll Number')
if 'First Name' in my_tuple:
    print("item exists")

Output:

item exists

Leave a Reply

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