Open In App

Tuples in Python

Last Updated : 04 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Python Tuple is a collection of objects separated by commas. A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable.

# Note : In case of list, we use square
# brackets []. Here we use round brackets ()
t = (10, 20, 30) 

print(t)
print(type(t))

Output
(10, 20, 30)
<class 'tuple'>

What is Immutable in Tuples?

Unlike Python lists, tuples are immutable. Some Characteristics of Tuples in Python.

  • Like Lists, tuples are ordered and we can access their elements using their index values
  • We cannot update items to a tuple once it is created. 
  • Tuples cannot be appended or extended.
  • We cannot remove items from a tuple once it is created. 

Let us see this with an example.

t = (1, 2, 3, 4, 5)

# tuples are indexed
print(t[1])
print(t[4])

# tuples contain duplicate elements
t = (1, 2, 3, 4, 2, 3)
print(t)

# updating an element
t[1] = 100
print(t)

Output:

2
5
(1, 2, 3, 4, 2, 3)
Traceback (most recent call last):
File "Solution.py", line 12, in <module>
t[1] = 100
TypeError: 'tuple' object does not support item assignment

Accessing Values in Python Tuples

Tuples in Python provide two ways by which we can access the elements of a tuple.

Python Access Tuple using a Positive Index

Using square brackets we can get the values from tuples in Python.

t = (10, 5, 20)

print("Value in t[0] = ", t[0])
print("Value in t[1] = ", t[1])
print("Value in t[2] = ", t[2])

Output
Value in t[0] =  10
Value in t[1] =  5
Value in t[2] =  20

Access Tuple using Negative Index

In the above methods, we use the positive index to access the value in Python, and here we will use the negative index within [].

t = (10, 5, 20)

print("Value in t[-1] = ", t[-1])
print("Value in t[-2] = ", t[-2])
print("Value in t[-3] = ", t[-3])

Output
Value in t[-1] =  20
Value in t[-2] =  5
Value in t[-3] =  10

Below are the different operations related to tuples in Python:

Traversing Items of Python Tuples

Like List Traversal, we can traverse through a tuple using for loop.

# Define a tuple
t = (1, 2, 3, 4, 5)

# Traverse through each item in the tuple
for x in t:
    print(x, end=" ")

Output
1 2 3 4 5 

Concatenation of Python Tuples

To Concatenation of Python Tuples, we will use plus operators(+).

# Code for concatenating 2 tuples
t1 = (0, 1, 2, 3)
t2 = ('python', 'geek')

# Concatenating above two
print(t1 + t2)

Output
(0, 1, 2, 3, 'python', 'geek')

Nesting of Python Tuples

A nested tuple in Python means a tuple inside another tuple.

# Code for creating nested tuples
t1 = (0, 1, 2, 3)
t2 = ('python', 'geek')

t3 = (t1, t2)
print(t3)

Output
((0, 1, 2, 3), ('python', 'geek'))

Repetition Python Tuples

We can create a tuple of multiple same elements from a single element in that tuple.

# Code to create a tuple with repetition
t = ('python',)*3
print(t)

Output
('python', 'python', 'python')

Try the above without a comma and check. You will get tuple3 as a string ‘pythonpythonpython’. 

Slicing Tuples in Python

Slicing a Python tuple means dividing a tuple into small tuples using the indexing method. In this example, we slice the tuple from index 1 to the last element. In the second print statement, we printed the tuple using reverse indexing. And in the third print statement, we printed the elements from index 2 to 4.

# code to test slicing
t = (0 ,1, 2, 3)

print(t[1:])
print(t[::-1])
print(t[2:4])

Output
(1, 2, 3)
(3, 2, 1, 0)
(2, 3)

Note: In Python slicing, the end index provided is not included.

Deleting a Tuple in Python

In this example, we are deleting a tuple using ‘del’ keyword. The output will be in the form of error because after deleting the tuple, it will give a NameError.

Note: Remove individual tuple elements is not possible, but we can delete the whole Tuple using Del keyword.

# Code for deleting a tuple
t = ( 0, 1)

del t
print(t)

Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 5, in <module>
print(t)
NameError: name 't' is not defined

Finding the Length of a Python Tuple

To find the length of a tuple, we can use Python’s len() function and pass the tuple as the parameter.

# Code for printing the length of a tuple
t = ('python', 'geek')
print(len(t))

Output
2

Multiple Data Types With Tuple

Tuples in Python are heterogeneous in nature. This means tuples support elements with multiple datatypes.

# tuple with different datatypes
t = ("immutable", True, 23)
print(t)

Output
('immutable', True, 23)

Converting a List to a Tuple

We can convert a list in Python to a tuple by using the tuple() constructor and passing the list as its parameters.

# Code for converting a list and a string into a tuple
a = [0, 1, 2]
t = tuple(a)

print(t)

Output
(0, 1, 2)

Output:

Tuples take a single parameter which may be a list, string, set, or even a dictionary(only keys are taken as elements), and converts them to a tuple.

Tuples in a Loop

We can also create a tuple with a single element in it using loops.

# python code for creating tuples in a loop
t = ('gfg',)

# Number of time loop runs
n = 5 
for i in range(int(n)):
    t = (t,)
    print(t)

Output
(('gfg',),)
((('gfg',),),)
(((('gfg',),),),)
((((('gfg',),),),),)
(((((('gfg',),),),),),)

Different Ways of Creating a Tuple

  • Using round brackets
  • Without Brackets
  • Tuple Constructor
  • Empty Tuple
  • Single Element Tuple
  • Using Tuple Packing

Using Round Brackets

t = ("gfg", "Python") 
print(t)

Output
('gfg', 'Python')

Using Comma Separated

# Creating a tuple without brackets
t = 4, 5, 6
print(t)  # Output: (4, 5, 6)

Output
(4, 5, 6)

Using Tuple Constructor

# Creating a tuple using the tuple() constructor
t = tuple([7, 8, 9])
print(t)  # Output: (7, 8, 9)

Output
(7, 8, 9)

Creating an Empty Tuple

# Creating an empty tuple
t = ()
print(t)  # Output: ()

Output
()

Single Element Tuple

# Creating a single-element tuple
t = (10, ) # Comma is important here
print(t)  # Output: (10,)
print(type(t))

# What if we do not use comma
t = (10) # This an integer (not a tuple)
print(t)  
print(type(t))

Output
(10,)
<class 'tuple'>
10
<class 'int'>

Tuple Packing

# Tuple packing
a, b, c = 11, 12, 13
t = (a, b, c)
print(t)  # Output: (11, 12, 13)

Output
(11, 12, 13)




Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg