AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Tuples in Python: Unmodifiable Lists

PARTAGEZ

Tuples in Python (“n-tuples” in French) are collections of different or identical data which are designated by an index and which cannot be modified (we say that they are “non-mutable”). Python tuples are created like classic lists in the web programming language.

Web hosting with personal advisor!

Powerful, flexible and high-performance web hosting with mailbox, personal advisor and domain included 1time year !

Domain
SSL certificate
24/7 Support

What are tuples in Python?

At first glance, tuples in Python look like well-known examples of Python lists. Both are lists that contain a lot of different content, can store it and thus present it in a structured way. But one difference remains: the content of a Python tuple is immutable (unmodifiable). In other words, once the Python tuples are filled with content, it remains the same and cannot be deleted. This is particularly useful if this content is composed of constants that must be kept in their current form and composition. When you use Python tuples, you can be assured that their content is presented in its original form.

How are Python tuples created?

Python tuples are declared in parentheses and their contents are separated by commas. The print function is used for output. A tuple is written as follows:

tup = ("première_valeur", "deuxième_valeur", "troisième_valeur")
print(tup)

It is also possible to declare a tuple with a single element. In this case, however, it is important to place a comma behind the element, otherwise the program cannot recognize a tuple and instead interprets it as a string in parentheses. Here is the correct notation in such a case:

tup = ("valeur_unique",)
print(tup)

Examples of Python tuples

If you want to create Python tuples in this way, use parentheses for this purpose, and place all important information in the correct order. As mentioned earlier, Python tuples can contain different types of data. Simple Python strings are thus possible:

animaux = ("Chien", "Chat", "Souris")
chiffres = (4, 17, 39, 12)
booléens = (False, False, True, False)

A mix is ​​also possible:

personne = ("Jacques", "Dupond", 1974, True)

Addressing elements in Python tuples

Since the individual elements of a Python tuple are defined by an index, they can be addressed and read without difficulty. The process is similar to a normal list and is started by hooks. For instance :

animaux = ("Chien", "Chat", "Souris")
print(animaux[1])

The second value of this Python tuple is « cat », so the word is returned after this query.

Follow these steps to read multiple values:

animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
print(animaux[1:3])

The output is now:

('Chat', 'Souris', 'Serpent')

Determine the length of a Python tuple

To determine the length of a Python tuple, use the len function. It works as follows:

animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
print(len(animaux))

On output, you will get a « 5 » for the five values ​​contained.

Edit Python tuples

Python tuples cannot be modified or enriched with new elements. To update an existing tuple, you can create a new tuple and fill it with new values ​​and the original tuple. This works with the operator +. Here is an example:

quelques_animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
tous_animaux = quelques_animaux + ("Hamster", "Girafe")
print(tous_animaux)

The output is now:

Convert Python tuples to lists

However, if some values ​​of a Python tuple are no longer up to date, the method explained above does not allow you to move forward either. You have to resort to an alternative because it is not possible to modify the values ​​in a Python tuple. The classic list offers this possibility. By converting a tuple to a list, you can modify the existing values ​​again. It works as follows:

animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
liste_animaux = list(animaux)
liste_animaux[2] = "Éléphant"
print(tier_liste)

We now have the following result:

['Chien', 'Chat', 'Éléphant', 'Serpent', 'Cheval']

Convert Lists to Python Tuples

But it also works in reverse. If you have created a list and want to keep the corresponding values ​​in their current form, you can also convert lists to Python tuples. In the following example, we convert a list to a tuple and then query the Python tuple’s data type for safety:

liste_couleur = ["bleu", "rouge", "jaune", "orange"]
couleurs = tuple(liste_couleur)
print(couleurs)
print(type(couleurs))

The output then looks like this:

("bleu", "rouge", "jaune", "orange")
<class 'tuple'=""></class>

To delete a tuple

Although you cannot remove individual items from a tuple, you can still remove the entire uneditable list. To do this, use the led command. Here is the corresponding code:

animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
del animaux

Integrated functions

Only two built-in functions for Python tuples exist: index and count.

Index function for Python tuples

You question theindex of a specific value with index. Here is the corresponding code:

animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
animaux.index("Serpent")

In this case, the output would be « 3 ».

The count function for Python tuples

With count you can query how often an item occurs in the Python tuple. Here is an example:

animaux = ("Chien", "Chat", "Souris", "Serpent", "Cheval")
animaux.count("Chien")

In this case, the answer is “1”.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact