Python tuples

Python tuples are collections of the same data or different data which have an index and cannot be changed. Python tuples are created like classical lists in the internet programming language.

Web hosting with a personal consultant

Fast and scalable, including a free domain for the first year and email address, trust web hosting from IONOS!

Wildcard SSL
24/7 support

What are Python tuples?

At first glance, Python tuples resembles Python lists. Both are lists which contain and store many different types of content. They enable clear organization of the content stored. However, the main difference is that the contents in a Python tuple are immutable. This means that once a Python tuple has been filled with content, it remains unchanged and cannot be deleted. This is useful if this content contains constants which must be kept in their current form and composition. Therefore, Python tuples ensures that content stays in its original form.

The contents of a Python tuple are written in a fixed order, which also cannot be changed afterwards. Each individual value is assigned an index number starting at 0, which you may know already from Python tutorials. The individual contents within the Python tuple may have the same values, as they distinguishable by the index numbers. The length of a Python tuple is not limited and its contents may theoretically consist of all possible data types.

How are Python tuples created?

Python tuples are formed with round braces and their contents are separated by commas. The print function is used for the output. A tuple is written as follows:

tup = ("first_value", "second_value", "third_value")
print (tup)

It is also possible to form a tuple with a single element. However, it is important that a comma is placed after the element, otherwise the program will not be able to recognize the tuple and will assume that it is a string with round braces instead. This is the correct code:

tup = ("single_value".)
print (tup)

Python Tuples examples

Python tuples require round braces. Important information must also be put in the right order. As we have already mentioned, Python tuples can contain different data types. Simple Python strings are possible:

animals = ("dog", "cat", "mouse")

Numerical values:

numbers = (4, 17, 39, 12)

Boolean values:

booleans = (false, false, true, false)

A mixture is also possible:

person = ( Smith", "John", 1974, True)

How do I address elements in Python tuples?

Individual elements of a Python tuple can be easily addressed and read since they are specified with an index. The process is similar to a normal list and can be done with square braces. See the following example:

animals = ("dog", "cat", "mouse")

"cat" is the second value in this Python tuple, so this word will be output following this request.

Proceed as follows if you want to read several values:

animals = ("dog", "cat", "mouse", "snake", "horse")

The output will now read:

(‘cat’, ‘mouse’, ‘snake’)

How do I determine the length of a Python tuple?

Use the len function to find the length of a Python tuple. It works like this:

animals = ("dog", "cat", "mouse", "snake", "horse")

You will get a “5” as the output for the five values.

How do I change Python tuples?

Python tuples are immutable and new elements cannot be added. However, to update an existing tuple, you can create a new tuple and fill it with new values and the original tuple. You can do this with the + operator. Below is an example:

some_animals = ("dog", "cat", "mouse", "snake", "horse")
all_animals = some_animals + ("hamster", "giraffe")
print (all_animals)

The output should now read:

How do I convert Python tuples to lists?

However, changing Python tuples is not possible if certain values are no longer up to date, so you’ll need an alternative method to update the values within a Python tuple. The classic list is the solution. Converting a Python tuple into a list allows you to change existing values. It works like this:

animals = ("dog", "cat", "mouse", "snake", "horse")
animal_list = list(animals)
animal_list[2] = "elephant"

We’ll get this as an output now:

['dog', 'cat', 'elephant', 'snake', 'horse']

How do I convert lists to Python tuples?

This method also works in reverse. You can also convert lists to Python tuples if you have created a list and would like to keep the values in their current form. The following example shows a list being converted to a tuple and queries the Python tuple’s data type for safety:

color_list = ["blue", "red", "yellow", "orange"]
colors = tuple(color_list)

The output will look like this:

("blue", "red", "yellow", "orange")
< class 'tuple' >

How do I delete a tuple?

You cannot remove individual elements from a tuple, but you can delete the entire immutable list. Use the del command to do this. Here is the code:

animals = ("dog", "cat", "mouse", "snake", "horse")
del animals

What are integrated functions?

There are only two built-in functions for Python tuples: index and count.

The index function for Python tuples

Use index to query the index of a particular value. Here is the code:

animals = ("dog", "cat", "mouse", "snake", "horse")
animals.index ("snake")

The output would be "3" in this case.

The count function for Python tuples

Use count to query how often an element occurs within the Python tuple. Below is an example:

animals = ("dog", "cat", "mouse", "snake", "horse")
animals.count ("dog")

The answer is "1" in this case.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.