A tuple is a immutable list of values. Tuples are one of Python's simplest and most common collection types, and can be created with the comma operator (
value = 1, 2, 3).
(1, a, "hello") # a must be a variable
() # an empty tuple
(1,) # a 1-element tuple.
(1)is not a tuple.
1, 2, 3 # the 3-element tuple (1, 2, 3)
Parentheses are only needed for empty tuples or when used in a function call.
A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable and are hashable, so they can be used in sets and maps
Built-in Tuple Functions
Tuples support the following build-in functions
If elements are of the same type, python performs the comparison and returns the result. If elements are different types, it checks whether they are numbers.
- If numbers, perform comparison.
- If either element is a number, then the other element is returned.
- Otherwise, types are sorted alphabetically .
If we reached the end of one of the lists, the longer list is "larger." If both list are same it returns 0.
len returns the total length of the tuple
Max of a tuple
max returns item from the tuple with the max value
Min of a tuple
The function min returns the item from the tuple with the min value
Convert a list into tuple
The built-in function
tuple converts a list into a tuple.
+ to concatenate two tuples
Indexing with negative numbers will start from the last element as -1:
Indexing a range of elements
Packing and Unpacking Tuples
Tuples in Python are values separated by commas. Enclosing parentheses for inputting tuples are optional, so the two assignments
a = 1, 2, 3 is also called packing because it packs values together in a tuple.
Note that a one-value tuple is also a tuple. To tell Python that a variable is a tuple and not a single value you can use a trailing comma
A comma is needed also if you use parentheses
To unpack values from a tuple and do multiple assignments use
_ can be used as a disposable variable name if one only needs some elements of a tuple, acting as a placeholder:
Single element tuples:
Reverse elements within a tuple
Or using reversed (reversed gives an iterable which is converted to a tuple):
Syntactically, a tuple is a comma-separated list of values:
Although not necessary, it is common to enclose tuples in parentheses:
Create an empty tuple with parentheses:
To create a tuple with a single element, you have to include a final comma:
Note that a single value in parentheses is not a tuple:
To create a singleton tuple it is necessary to have a trailing comma.
Another way to create a tuple is the built-in function
These examples are based on material from the book Think Python by Allen B. Downey.
Tuple Are Element-wise Hashable and Equatable
Thus a tuple can be put inside a
set or as a key in a
dict only if each of its elements can.
Tuples are immutable
One of the main differences between
tuples in Python is that tuples are immutable, that is, one cannot add or modify items once the tuple is initialized. For example:
Similarly, tuples don't have
.extend methods as
list does. Using
+= is possible, but it changes the binding of the variable, and not the tuple itself:
Be careful when placing mutable objects, such as
lists, inside tuples. This may lead to very confusing outcomes when changing them.
Will both raise an error and change the contents of the list within the tuple:
You can use the
+= operator to "append" to a tuple - this works by creating a new tuple with the new element you "appended" and assign it to its current variable; the old tuple is not changed, but replaced!
This avoids converting to and from a list, but this is slow and is a bad practice, especially if you're going to append multiple times.