Variables can be of the datatype tuple
. A tuple is collection that cannot be modified.
It differs from a list, in that lists can be changed or modified after creation.
A tuple is defined using parenthesis (
and )
instead of [
and ]
that a list uses.
If you want to change the data during program execution, use a list instead of a tuple.
Related Course: Complete Python Programming Course & Exercises
Python tuple
A tuple is an immutable sequence type. A tuple is a sequence, but its values cannot be altered.
The values of a tuple cannot change. This is very important, because this is the only difference between a list.
To create a tuple, all you need to do is define a set of comma-separated values between the parenthesis.
An empty tuple would be defined as:
tup1 = () |
be careful with parenthesis,
()
is a tuple and[]
is a list and{}
a dictionary
A tuple with one item ( a comma is needed in the end ):
x = (1,) |
x = (1,2,3,4) |
You can create all kinds of tuples:
t1 = ('physics', 'chemistry', 'geography', 'history') |
Note: Even if your tuple has only one element, the comma is needed between the parenthesis.
Related Course: Complete Python Programming Course & Exercises
Accessing tuples
To access individual elements, we use square brackets. To print the first element (Python starts counting from zero):print(x[0])
print(x[1])
You can access tuples both in the interpreter and during code execution.
In the interpreter python
, you can type commands.
Lets create a tuple
'coffee','tea','orange juice','lemonade') t1 = ( |
Then output some elements
0] t1[ |
And why not try to change a value
1] = 'water' t1[ |
Remember, the contents of a tuple cannot be changed after creation.
To print the last element, you can count from the back using the minus sign.print(x[-1])
Related Course: Complete Python Programming Course & Exercises
Tuple Unpacking
You can unpack a tuple. A package tuple object has more than one value like:
1,2,3) t = ( |
To unpack the tuple you can do this:
x,y,z = t |
Then each variable (x,y,z) will contain one of the tuples values:
x |
When unpacking, the number of variables must match the number of values in the tuple:
1,2,3) t = ( |
Related Course: Complete Python Programming Course & Exercises
Tuple assignment
If you define a tuple, Python allows the parenthesis to be left out. In Python these are the same:
1,2,3) t = ( |
So you can do unpacking like this:
1,2,3 t = |
So it works both if parenthesis are written or left out. For clarity, you could add them.
When in doubt, you can always call type()
.
type(t) |
Python tuple append
Once defined, the contents of a tuple cannot be changed.
1,2,3) t = ( |
That is by design. If you want to append values, you should use a list.
Note: Tuples cannot be changed. But what you can do, is create a new tuple and overwrite it.
However, you can add a tuple to a tuple:
4,) t = t + ( |
A new element has been appended at the end of the tuple, by appending two tuples.
Related Course: Complete Python Programming Course & Exercises
Python tuple insert
Start with a tuple, like this:
1,2,4,5) t = ( |
To insert a new tuple, create three tuples and the tuple to insert will go in the middle.
So you can use tuple slicing [start:end]
and combine.
The example below shows how a new tuple is created with a new element in the middle:
0:2] + (3,) + t[2:4] t = t[ |
Python tuple modify
The contents of a tuple cannot be changed. If you try it will throw an TypeError.
1] = 2 t[ |
You can get around this by creating a new tuple.
1,2,3,4,5) t = ( |
Python tuple delete
An element cannot be deleted. You can only create a new tuple without that element.
If you have this tuple:
1,2,3,4,5) t = ( |
You can create a new one using slicing:
# Create new tuple |
If you want to change the contents of a tuple (append, delete, modify,…) you should use a list
instead.
Python tuple to list
To convert a tuple to a list, call the list()
function.
# create tuple |
# convert tuple to list |
If you output mylist, you’ll see its a list:
mylist |
You can see the difference between lists and tuples by their parenthesis.
- Tuples use
()
- lists uses
[]
.
Python list to tuple
A list can be converted to a tuple by calling the tuple()
function:
t = tuple(mylist) |
Variable t is then a tuple:
t |
If you are a Python beginner, then I highly recommend this book.
what's the difference between Lists and tuples? Is the tuples similar with enumeration of C language?
Tuples cannot be modified once created. The only way to change a tuple is to create a new one and replace the existing one.
Hi,
I don't understand why the last numeric character in the multiple tuple does not have a comma after it?
If it is a stand alone single character, it has a comma; and yet if it is the last numeric character in a string of numerics, then each numeric has a comma after it - with the exception of the last numeric, why?
A comma on the end is required with a single element, but not if you have multiple elements. A tuple is defined as: values separated by commas.
Why a comma for one element? Python has otherwise no way of knowing its a tuple, consider:
Python wouldn't know if it's a tuple, in this case it's just a number. That's why a tuple always needs at least one comma. If we have more than one number, it already has commas.