Tuples#
Introduction to Tuples#
A tuple
is an ordered collection of objects, similar to a list
.
However, tuples have some distinct characteristics that make them useful in specific scenarios.
Defining a Tuple#
Tuples are defined using parentheses (round brackets) ()
instead of the square brackets []
used for lists.
noble_gases = ('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon')
We can check noble_gases
is a tuple, using the type()
function:
type(noble_gases)
tuple
Accessing Tuple elements#
Like lists, we can use the index notation to access particular elements in a tuple.
print(noble_gases[0])
helium
print(noble_gases[-1])
radon
Tuples are Immutable#
The main difference between a tuple and a list is that tuples are immutable: once created, they cannot be altered.
noble_gases[0] = 'Helium'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 noble_gases[0] = 'Helium'
TypeError: 'tuple' object does not support item assignment
However, you can still concatenate tuples:
new_noble_gases = noble_gases + ('oganesson',)
print(new_noble_gases)
('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon', 'oganesson')
Note the trailing comma in ('oganesson',)
— this is necessary for Python to recognise this as a tuple with one element.
Tuple Properties#
Heterogeneous Data: Tuples can contain items of different data types.
my_tuple = (0, 'a', 2.3)
print(my_tuple)
(0, 'a', 2.3)
Conversion: You can convert a tuple to a list and vice versa:
my_list = list(my_tuple)
print(my_list)
[0, 'a', 2.3]
Tuples vs Lists: When to Use Each#
Python provides both tuples and lists to cater to different programming needs and scenarios.
Key Differences#
Mutability:
Lists are mutable (can be changed after creation).
Tuples are immutable (cannot be changed after creation).
Syntax:
Lists use square brackets:
[1, 2, 3]
.Tuples use round brackets:
(1, 2, 3)
.
Performance:
Tuples are generally more memory efficient and faster to process.
Advantages of Tuples#
Data Integrity: Immutability prevents accidental modifications.
Debugging: If you try to change a tuple, Python will raise an error, helping catch bugs early.
Guaranteed Consistency: The contents of a tuple remain the same throughout your code.
When to Choose Tuples#
Representing fixed collections of data (e.g., RGB color values).
Returning multiple values from a function.
When you want to ensure data doesn’t change accidentally.
As dictionary keys (when needed).
When to Choose Lists#
When you need a collection that will change size.
When you’ll frequently modify individual elements.
When you need list-specific methods (e.g.,
sort()
,append()
).
Conclusion#
In general, unless you know that you are going to want to do something with your data that you can only do with a list, it is often a good idea to use a tuple instead. This practice can lead to more efficient and less error-prone code. However, if you’re unsure or need the flexibility, starting with a list is often a safe choice. As you gain experience, you’ll develop an intuition for when each is most appropriate.