Tuple
PronunciationTUP-el
Definition
A tuple is an ordered, fixed collection of values. Once created, it cannot be changed.
Where you hear it
In Python code that returns several values, and in explanations of when to use a tuple instead of a list.
Examples
The function returns a tuple of the minimum and maximum.
A tuple of coordinates can be used as a dictionary key.
Return the result as a tuple, such as (min, max), so the caller can unpack both values.
Common mistake
Forgetting the comma in a one-item tuple: (5) is just the number 5, while (5,) is a tuple.
Don't confuse with
A list, which is changeable. Use a tuple for a fixed record and a list for a collection that grows.
Say it at work
- In a meeting or chat
Return a tuple so the caller can unpack both values at once.
- In an email or ticket
Make it a tuple, since these values should never change.
Keep exploring
More in Programming Fundamentals