VOOZH about

URL: https://www.geeksforgeeks.org/python/tuples-in-python/

⇱ Tuple Operations in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tuple Operations in Python

Last Updated : 23 Jul, 2025

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.


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.

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.


Output
Value in tup[0] = 10
Value in tup[1] = 5
Value in tup[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 [].


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

Different Operations Related to Tuples

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.


Output
1 2 3 4 5 

Concatenation of Python Tuples

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


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

Nesting of Python Tuples

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


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.


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.


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.

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.


Output
2

Multiple Data Types With Tuple

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


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.


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.


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


Output
('gfg', 'Python')

Using Comma Separated


Output
(4, 5, 6)

Using Tuple Constructor


Output
(7, 8, 9)

Creating an Empty Tuple


Output
()

Single Element Tuple


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

Tuple Packing


Output
(11, 12, 13)

Tuple Built-In Methods

Tuples support only a few methods due to their immutable nature. The two most commonly used methods are count() and index()

Built-in-MethodDescription
index( )Find in the tuple and returns the index of the given value where it's available
count( )Returns the frequency of occurrence of a specified value

Tuple Built-In Functions

Built-in FunctionDescription
all()Returns true if all element are true or if tuple is empty
any()return true if any element of the tuple is true. if tuple is empty, return false
len()Returns length of the tuple or size of the tuple
enumerate()Returns enumerate object of tuple
max()return maximum element of given tuple
min()return minimum element of given tuple
sum()Sums up the numbers in the tuple
sorted()input elements in the tuple and return a new sorted list
tuple()Convert an iterable to a tuple.

Tuples VS Lists

SimilaritiesDifferences

Functions that can be used for both lists and tuples:

len(), max(), min(), sum(), any(), all(), sorted()

Methods that cannot be used for tuples:

append(), insert(), remove(), pop(), clear(), sort(), reverse()

Methods that can be used for both lists and tuples:

count(), Index()

we generally use 'tuples' for heterogeneous (different) data types and 'lists' for homogeneous (similar) data types.
Tuples can be stored in lists.Iterating through a 'tuple' is faster than in a 'list'.
Lists can be stored in tuples.'Lists' are mutable whereas 'tuples' are immutable.
Both 'tuples' and 'lists' can be nested.Tuples that contain immutable elements can be used as a key for a dictionary.
Comment
Article Tags:
Article Tags: