AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python Enumerate (): the function for enumerations

PARTAGEZ

Python enumerate() is a function used to associate a digital index with each element of a sequence. It is useful for numbering chains and enumerations.

What is the Python function enumerate() ?

Python function enumerate() is used for make an enumeration from an entrance. This function attributes a digital index to each element of a sequence, such as a character string or a python. The enumeration is continuous and begins by default at « 0 ». This function is included by default in this programming language.

Managed NextCloud of Ionos Cloud

Work as a team in your own cloud

  • Data security
  • Integrated collaboration tools
  • Accommodation in European data centers

Python syntax and parameters enumerate()

The Syntax of Python Enumerate () is as follows:

enumerate(iterable, start)

python

She has two parameters:

  • iterable : This is an object or a sequence that can be executed in a loop. This parameter will then be placed before enumeration. It is compulsory.
  • start : This parameter is optional. It allows you to determine from which numerical value the numbering must begin. Its default is « 0 ».

Example of enumeration with enumerate()

To illustrate the functioning of the function, we will choose a simple example with four different colors. We number them using the function enumerate() of Python. The corresponding code presents itself as follows:

couleurs = ['bleu', 'rouge', 'jaune', 'orange']
séquence = enumerate(couleurs)
print(list(séquence))

python

It gives us this outing:

[(0, 'bleu'), (1, 'rouge'), (2, 'jaune'), (3, 'orange')]

python

Python enumerate() with a starting index

As we have not specified a starting index, the list begins at « 0 ». To change this, we add to Python enumerate() THE « Start » parameter with the value « 1 ». The previous code should only be slightly changed:

couleurs = ['bleu', 'rouge', 'jaune', 'orange']
séquence = enumerate(couleurs, 1)
print(list(séquence))

python

Our outing is immediately improved:

[(1, 'bleu'), (2, 'rouge'), (3, 'jaune'), (4, 'orange')]

python

You can change the starting index as you wish: the enumeration can start at any other value.

The function enumerate() with a loop for

It is possible to combine a fork loop, with python enumerate()whether it is With or without starting index. For the first loop, we will not use the « Start » parameter. The counting therefore begins at « 0 ». For the second loop, we will add the parameter with the starting point « 5 ». The counting therefore starts from this point. With the indicator "\n"we ask the system to create a new line each time to make things a little clearer. Here is the code of this combination:

couleurs = ['bleu', 'rouge', 'jaune', 'orange']
for séquence in enumerate(couleurs):
    print(séquence)
    print("\n")
print("Ça reprend à partir de 5")     
for séquence in enumerate(couleurs, 5):
    print(séquence)
    print("\n")

python

Our outing now looks like this:

(0, 'bleu')
(1, 'rouge')
(2, 'jaune')
(3, 'orange')
Ça reprend à partir de 5
(5, 'bleu') 
(6, 'rouge')
(7, 'jaune')
(8, 'orange')

python

List a chain with python enumerate()

If you want to list a character string, Python enumerate() is also a good choice. The chain is broken down and numbered. Here is what the corresponding code looks like:

chaine = "exemple"
for x in enumerate(chaine, 1):
    print(x)

python

The exit will be as follows:

(1, 'e')
(2, 'x')
(3, 'e')
(4, 'm')
(5, 'p')
(6, 'l')
(7, 'e')

python

Advice

The best solution for websites and applications: With Deploy Now from Ionos, you deploy your web projects directly via Github. Not only do you benefit from a reasonable price, but you can also adapt the configuration to your needs. Ask our experts for advice!

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

Web Marketing

AI security: issues, risks and best practices

AI security of autonomous agents and systems aims to protect these technologies against targeted attacks, manipulation and unintentional malfunctions. Unlike traditional IT security, AI security

Web Marketing

Screenshot tools: the best free programs

Depending on the possibilities offered by your operating system, it is possible to take perfect screenshots. However, it can be tedious to open them in

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact