In Python, tuples are immutable sequences, meaning a tuples elements cannot be modified, added, or removed after creation.
The immutability of tuples makes them faster and more memory-efficient compared to lists, which are mutable. This property makes them ideal for storing data that should not change throughout the program.
Creating Tuples
Tuples can be created in Python in several ways.
Creating a tuple using parentheses ()
:
pythontest_tuple = (1, 2, 3)
Creating a tuple without parentheses:
pythontest_tuple = 1, 2, 3
Creating a tuple with 1 element:
pythontest_tuple = (1,)
Note the comma
,
that appears after the element. This is essential because without the comma, Python will interpret it as a regular number surrounded by parentheses and not a tuple
Creating a tuple using the tuple()
constructor:
pythonmy_tuple = tuple([1, 2, 3])
Creating an empty tuple:
pythonempty_tuple = tuple() # or empty_tuple = ()
Accessing Tuple Elements
Tuple elements can be accessed in the same way that list elements can be accessed in Python.
Accessing an individual element of a tuple with indexing:
pythonmy_tuple = (10, 20, 30, 40) print(my_tuple[0]) # Output: 10 print(my_tuple[-1]) # Output: 40
Accessing multiple elements of a tuple with slicing:
pythonmy_tuple = (10, 20, 30, 40, 50) print(my_tuple[1:3]) # Output: (20, 30) print(my_tuple[:3]) # Output: (10, 20, 30) print(my_tuple[3:]) # Output: (40, 50)
Assigning the elements of a tuple to multiple variables in a single statement:
pythonpoint = (10, 20) x, y = point print(x) # Output: 10 print(y) # Output: 20
Working Around Tuple Immutability
While you cannot directly modify elements of a tuple, you can create a new tuple that is a modified version of the original.
pythonmy_tuple = (1, 2, 3) new_tuple = my_tuple + (4, 5) # Adding an element by creating a new tuple print(new_tuple) # Output: (1, 2, 3, 4, 5)
Tuple Operations
Even though tuples are immutable, you can still perform several operations on them, like concatenation, repetition, membership tests, and finding a tuples length.
Concatenating two tuples together with the +
operator:
pythontuple1 = (1, 2) tuple2 = (3, 4) combined = tuple1 + tuple2 print(combined) # Output: (1, 2, 3, 4)
Repeating the elements in a tuple using the *
operator:
pythonmy_tuple = (1, 2, 3) repeated = my_tuple * 3 print(repeated) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
Checking if an element is in a tuple using the in
operator:
pythonmy_tuple = (1, 2, 3) print(2 in my_tuple) # Output: True print(4 in my_tuple) # Output: False
Finding the number of elements in a tuple using the len()
function:
pythonmy_tuple = (1, 2, 3) print(len(my_tuple)) # Output: 3
Nested Tuples
A unique feature of tuples in Python is that they can contain other tuples, lists strings, numbers, and so on as elements.
pythonnested_tuple = (1, 2, (3, 4), [5, 6], "word")
Tuple Methods
Since tuples are immutable, there are less methods available to use on them.
The only methods available for tuples are: count()
and index()
.
Counting the number of occurrences in a tuple using the count()
method:
pythonmy_tuple = (1, 2, 2, 3, 2) print(my_tuple.count(2)) # Output: 3
Finding the index of the first occurrence of a specified element:
pythonmy_tuple = (1, 2, 3, 4, 2) print(my_tuple.index(2)) # Output: 1
When To Use Tuples Vs. Lists
Understanding when to use a tuple or a list is very important when programming in Python.
To know when to use a tuple or a list, you need to understand the properties of them both.
- Tuples are immutable:
- If you expect the elements to need to be modified after creation, do not use a tuple
- If you don’t want the elements to be changed, tuples immutability adds a layer of protection from accidental modification
- Tuples consume less memory and are faster:
- If speed and memory use is an important factor in your program, it would be beneficial to use tuples
- Tuples can store different data types together:
- If you want to be able to store different data types together in the same collection, tuples are typically used
Conclusion
Tuples are a powerful and efficient data structure in Python since they are immutable, consume less memory, and are faster. When storing fixed collections of items that will not be expected to change, tuples are the most appropriate choice. Utilizing tuples properly will allow you to program faster and less memory-consuming programs.