Tuple

Last updated: Apr 12, 2026

Tuple


Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by `'commas'`. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily. Tuple is a collection which is ordered and immutable. Allows duplicate members.
Creating a tuple: Tuples are created by placing a sequence of values separated by `'comma'` with or without the use of parentheses for grouping the data sequence. Creation of tuples without the use of parentheses is known as Tuple Packing. To determine how many items a tuple has, use the `len()` function. To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple. Tuple items can be of any data type. A tuple can contain different data types. It is also possible to use the `tuple()` constructor to make a tuple.

Example:

# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)

# Creating a Tuple with the use of string
Tuple1 = ('Python', 'Tutorial')
print("\nTuple with the use of String: ")
print(Tuple1)

# Creating a Tuple with the only one tuple
Tuple1 = ('LIBED',)
print("\nTuple with the use of String: ")
print(Tuple1)
print(type(Tuple1), "\n", len(Tuple1))

# Creating a Tuple with the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))

# Creating a Tuple with the use of built-in function
Tuple1 = tuple('Python')
print("\nTuple with the use of function: ")
print(Tuple1)

Output:

Initial empty Tuple:
()

Tuple with the use of String:
('Python', 'Tutorial')

Tuple with the use of String:
('LIBED',)
<class 'tuple'>
 1

Tuple using List:
(1, 2, 4, 5, 6)

Tuple with the use of function:
('P', 'y', 't', 'h', 'o', 'n')

Creating a tuple with mixed data types: Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing 'comma' to make it a tuple.

Example:

# Creating a Tuple with Mixed Datatype
Tuple1 = (5, 'Welcome', 7, 'LIBED')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)

# Creating a Tuple with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'Tutorial')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)

Output:

Tuple with Mixed Datatypes:
(5, 'Welcome', 7, 'LIBED')

Tuple with nested tuples:
((0, 1, 2, 3), ('python', 'Tutorial'))

Accessing of tuples: Tuples are immutable, and usually, they contain a sequence of heterogeneous elements that are accessed via unpacking or indexing (or even by attribute in the case of named tuples).

Example:

# Accessing Tuple with Indexing
Tuple1 = tuple("LIBED")
print("\nFirst element of Tuple: ")
print(Tuple1[0])

# Tuple unpacking
Tuple1 = ("LIBED", "Python", "Tutorial")
# This line unpack values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)

Output:

First element of Tuple:
L

Values after unpacking:
LIBED
Python
Tutorial

Concatenation of tuples: Concatenation of tuples is the process of joining two or more Tuples. Concatenation is done by the use of '+' operator. Concatenation of tuples is done always from the end of the original tuple. Other arithmetic operations do not apply on Tuples.

Example:

# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ("LIBED", "PYTHON", "TUTORIAL")
Tuple3 = Tuple1 + Tuple2
print("\nTuples after Concatenation: ")
print(Tuple3)

Output:

Tuples after Concatenation:
(0, 1, 2, 3, 'LIBED', 'PYTHON', 'TUTORIAL')

Slicing of tuple: Slicing of a Tuple is done to fetch a specific range or slice of sub-elements from a Tuple. Slicing can also be done to lists and arrays. Indexing in a list results in fetching a single element whereas Slicing allows fetching a set of elements.

Example:

# Slicing of a Tuple with Numbers
Tuple1 = tuple('LIBED')

# Removing First element
print("Removal of First Element: ")
print(Tuple1[1:])

# Reversing the Tuple
print("\nTuple after sequence of Element is reversed: ")
print(Tuple1[::-1])

# Printing elements of a Range
print("\nPrinting elements between Range 4-9: ")
print(Tuple1[2:5])

Output:

Removal of First Element:
('I', 'B', 'E', 'D')

Tuple after sequence of Element is reversed:
('D', 'E', 'B', 'I', 'L')

Printing elements between Range 4-9:
('B', 'E', 'D')

Deleting a tuple: Tuples are immutable and hence they do not allow deletion of a part of it. The entire tuple gets deleted by the use of del() method.

Example:

# Deleting a Tuple
Tuple1 = (0, 1, 2, 3, 4)
del Tuple1
print(Tuple1)

Output:

NameError: name 'Tuple1' is not defined

Converting list to a tuple: Takes 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.

Example:

# Code for converting a list and a string into a tuple
list1 = [0, 1, 2]
print(tuple(list1))
print(tuple('python'))

Output:

(0, 1, 2)
('p', 'y', 't', 'h', 'o', 'n')

Tuple Methods


Below are few Tuple Methods we commonly use:

count()

The count() method of Tuple returns the number of times the given element appears in the tuple. Syntax is tuple.count(element). The parameter element is the element that is to be counted.

Example:

# Creating tuples
Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)

# count the appearance of 3
n = Tuple1.count(3)
print('Count of 3 in Tuple1 is:', n)

Output:

Count of 3 in Tuple1 is: 3

index()

The index() method returns the first occurrence of the given element from the tuple. Syntax is tuple.index(element, start, end). Parameters are:

  • element — The element to be searched.
  • start (Optional) — The starting index from where the searching is started.
  • end (Optional) — The ending index till where the searching is done.

This method raises a ValueError if the element is not found in the tuple.

Example:

# Creating tuples
Tuple = (0, 1, 2, 3, 2, 3, 1, 3, 2)

# getting the index of 3 after 4th index
res = Tuple.index(3, 4)
print('First occurrence of 3 after 4th index is:', res)

# getting the index of 4
res = Tuple.index(4)

Output:

First occurrence of 3 after 4th index is: 5
ValueError: tuple.index(x): x not in tuple