DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on Edited on

Python Day 26: Tuples in Python – Immutable Sequence Data Type

A tuple is similar to a list, but there is one important difference:

  • A List is Mutable.
  • A Tuple is Immutable.

We can think of a tuple as a read-only version of a list because we cannot directly modify its elements after creating it.

1. What is a Tuple?

A tuple is a sequence data type in Python that stores multiple elements in a single variable.

Example:

t = 12345, 54321, 'hello!'

print(t)
print(t[0])
Enter fullscreen mode Exit fullscreen mode
Output:

(12345, 54321, 'hello!')
12345
Enter fullscreen mode Exit fullscreen mode

Notice that we created a tuple without using parentheses.

Python automatically treats comma-separated values as a tuple.

We can also create a tuple using parentheses:


t = (10, 20, 30)

print(t)
print(type(t))
Enter fullscreen mode Exit fullscreen mode
Output:

(10, 20, 30)
<class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

2. Important Properties of Tuples

Property Explanation
Ordered Elements maintain their insertion order
Indexed We can access elements using indexes
Duplicates allowed The same element can appear multiple times
Heterogeneous Different data types can be stored together
Immutable Elements cannot be directly changed after creation
Iterable We can iterate through tuple elements

Example:

t = (10, 20, 10, 'Hello', True, 5.2)

print(t)

Output:

(10, 20, 10, 'Hello', True, 5.2)

Here, the tuple contains integers, strings, a Boolean value, and a float.

3. Nested Tuples

A tuple can contain another tuple. This is called a nested tuple.

t = 12345, 54321, 'hello!'

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

print(u)
Enter fullscreen mode Exit fullscreen mode
Output:

((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
Enter fullscreen mode Exit fullscreen mode

Here, u contains two elements:

  1. The tuple t.
  2. Another tuple containing numbers from 1 to 5.

4. Tuple Is Immutable

Once a tuple is created, we cannot change its elements directly.

t = (10, 20, 30)

t[0] = 100

print(t)
Enter fullscreen mode Exit fullscreen mode
Output:

TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

This happens because tuples are immutable.

But tuples can contain mutable objects

A tuple itself is immutable, but it can contain mutable objects such as lists.

v = ([1, 2, 3], [3, 2, 1])

print(v)
Enter fullscreen mode Exit fullscreen mode
Output:

([1, 2, 3], [3, 2, 1])
Enter fullscreen mode Exit fullscreen mode

We cannot replace the list inside the tuple, but we can modify the contents of that list.

v = ([1, 2, 3], [3, 2, 1])

v[0].append(4)

print(v)
Enter fullscreen mode Exit fullscreen mode
Output:

([1, 2, 3, 4], [3, 2, 1])
Enter fullscreen mode Exit fullscreen mode

The tuple structure remains unchanged, but the mutable list inside it is modified.

5. Creating a Tuple with Different Data Types

A tuple can store different types of values.


t = 10, 20, 30, 'ABCD', True, 5.2

print(type(t))
print(len(t))
Enter fullscreen mode Exit fullscreen mode
Output:

<class 'tuple'>
6

Enter fullscreen mode Exit fullscreen mode

Here:

  • type() returns the data type.
  • len() returns the number of elements.

6. Tuple Unpacking

Tuple unpacking means assigning tuple elements to individual variables.

t = 10, 20, 30, 'ABCD', True, 5.2

e1, e2, e3, e4, e5, e6 = t

print(e4)
print(e6)
Enter fullscreen mode Exit fullscreen mode
Output:

ABCD
5.2
Enter fullscreen mode Exit fullscreen mode

Here, each tuple element is assigned to a corresponding variable.

7. Tuple Packing

Tuple packing means combining multiple values into a single tuple.

e1 = 10
e5 = True
e6 = 5.2

t2 = e1, e5, e6

print(t2)
Enter fullscreen mode Exit fullscreen mode

Output:

(10, True, 5.2)
Enter fullscreen mode Exit fullscreen mode

Here, three individual variables are packed into one tuple.

8. Creating a Tuple with a Single Element

This is an important concept.

no = 10

print(type(no))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Now let's add a comma:

no = 10,

print(type(no))
print(no)
Enter fullscreen mode Exit fullscreen mode
Output:

<class 'tuple'>
(10,)
Enter fullscreen mode Exit fullscreen mode

We can also use parentheses:

no = (10,)

print(type(no))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

Remember: A comma is necessary to create a single-element tuple. Parentheses alone are not enough.

no = (10)
print(type(no))  # int

no = (10,)
print(type(no))  # tuple
Enter fullscreen mode Exit fullscreen mode

9. Converting a List into a Tuple

We can convert a list into a tuple using the tuple() function.

l = [10, 20, 30]

t = tuple(l)

print(t)
print(type(t))
Enter fullscreen mode Exit fullscreen mode
Output:

(10, 20, 30)
<class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

Similarly, we can convert a tuple into a list using list().


t = (10, 20, 30)

l = list(t)

print(l)
print(type(l))
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 30]
<class 'list'>
Enter fullscreen mode Exit fullscreen mode

10. Creating a Tuple Using range()

We can create a tuple from a range of numbers.


l = list(range(1, 11))
print(l)

t = tuple(range(1, 11))
print(t)
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Enter fullscreen mode Exit fullscreen mode

Here:

  • list() converts the range into a list.
  • tuple() converts the range into a tuple.

11. Tuple Indexing and Slicing

Like lists and strings, tuples support indexing and slicing.

t = (10, 20, 30, 40, 50, 60, 70, 80, 90)

print(t[-1])
print(t[2:5])
print(t[2:8:2])
print(t[::3])

Enter fullscreen mode Exit fullscreen mode
Output:

90
(30, 40, 50)
(30, 50, 70)
(10, 40, 70)
Enter fullscreen mode Exit fullscreen mode

12. Tuple Operators

Tuples support operators such as + and *.

Concatenation (+)

The + operator combines two tuples.

t1 = (10, 20, 30)
t2 = (40, 50, 60)

print(t1 + t2)

Enter fullscreen mode Exit fullscreen mode
Output:

(10, 20, 30, 40, 50, 60)
Enter fullscreen mode Exit fullscreen mode

Repetition (*)

The * operator repeats the tuple elements.


t1 = (10, 20, 30)

print(t1 * 3)
Enter fullscreen mode Exit fullscreen mode

Output:

(10, 20, 30, 10, 20, 30, 10, 20, 30)
Enter fullscreen mode Exit fullscreen mode

13. Difference Between List and Tuple

Feature List Tuple
Syntax [10, 20, 30] (10, 20, 30)
Mutability Mutable Immutable
Insertion order Maintained Maintained
Indexing Supported Supported
Duplicates Allowed Allowed
Heterogeneous elements Allowed Allowed
Memory Generally more Generally less
Modification Elements can be changed Elements cannot be directly changed

14. Understanding id(), == and is

Let's understand the difference between == and is.

Example using a List


l1 = [10, 20, 30]
l2 = l1

print(id(l1))
print(id(l2))

l3 = list(l1)

print(id(l3))

print(l1 == l2)
print(l1 is l2)

print(l1 == l3)
print(l1 is l3)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • id() returns an object's identity.
  • == checks whether two objects have equal values.
  • is checks whether two variables refer to the same object.

Example using a Tuple


l1 = (10, 20, 30)
l2 = l1

print(id(l1))
print(id(l2))

l3 = tuple(l1)

print(id(l3))

print(l1 == l2)
print(l1 is l2)

print(l1 == l3)
print(l1 is l3)
Enter fullscreen mode Exit fullscreen mode

15. Tuple Methods and Built-in Functions

Tuples have only two built-in methods: count() and index()
Example:

t = (10, 20, 30, 10, 40, 10)

print(t.count(10))
print(t.index(30))
Enter fullscreen mode Exit fullscreen mode
Output:

3
2
Enter fullscreen mode Exit fullscreen mode
Method Purpose
count() Counts how many times an element occurs
index() Returns the index of the first occurrence

Other useful built-in functions work with tuples:len(),min(),max(),sorted()

Example:

t = (50, 10, 40, 20, 30)

print(len(t))
print(min(t))
print(max(t))
print(sorted(t))
Enter fullscreen mode Exit fullscreen mode
Output:

5
10
50
[10, 20, 30, 40, 50]
Enter fullscreen mode Exit fullscreen mode
Function Purpose
len() Returns the number of elements
min() Returns the smallest element
max() Returns the largest element
sorted() Returns a sorted list of elements

Note: sorted() returns a list, not a tuple. It does not modify the original tuple.

16. Tuple and Generator Expression

Consider this code:

t = (no for no in range(1, 11))

print(t)
Enter fullscreen mode Exit fullscreen mode

You might expect a tuple, but this actually creates a generator object.


t = (no for no in range(1, 11))

print(type(t))
Enter fullscreen mode Exit fullscreen mode
Output:

<class 'generator'>
Enter fullscreen mode Exit fullscreen mode

Why?

Because parentheses with a comprehension expression create a generator expression, not a tuple.

To convert the generator into a tuple, use tuple():

t = (no for no in range(1, 11))

print(tuple(t))
Enter fullscreen mode Exit fullscreen mode
Output:

(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Enter fullscreen mode Exit fullscreen mode

This is an important difference between tuple creation and generator expressions.

Top comments (0)