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])
Output:
(12345, 54321, 'hello!')
12345
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))
Output:
(10, 20, 30)
<class 'tuple'>
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)
Output:
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
Here, u contains two elements:
- The tuple t.
- 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)
Output:
TypeError: 'tuple' object does not support item assignment
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)
Output:
([1, 2, 3], [3, 2, 1])
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)
Output:
([1, 2, 3, 4], [3, 2, 1])
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))
Output:
<class 'tuple'>
6
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)
Output:
ABCD
5.2
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)
Output:
(10, True, 5.2)
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))
Output:
<class 'int'>
Now let's add a comma:
no = 10,
print(type(no))
print(no)
Output:
<class 'tuple'>
(10,)
We can also use parentheses:
no = (10,)
print(type(no))
Output:
<class 'tuple'>
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
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))
Output:
(10, 20, 30)
<class 'tuple'>
Similarly, we can convert a tuple into a list using list().
t = (10, 20, 30)
l = list(t)
print(l)
print(type(l))
Output:
[10, 20, 30]
<class 'list'>
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)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
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])
Output:
90
(30, 40, 50)
(30, 50, 70)
(10, 40, 70)
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)
Output:
(10, 20, 30, 40, 50, 60)
Repetition (*)
The * operator repeats the tuple elements.
t1 = (10, 20, 30)
print(t1 * 3)
Output:
(10, 20, 30, 10, 20, 30, 10, 20, 30)
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)
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)
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))
Output:
3
2
| 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))
Output:
5
10
50
[10, 20, 30, 40, 50]
| 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)
You might expect a tuple, but this actually creates a generator object.
t = (no for no in range(1, 11))
print(type(t))
Output:
<class 'generator'>
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))
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
This is an important difference between tuple creation and generator expressions.
Top comments (0)