Python enumerate() is used to output an input as an enumeration object. You can use the integrated function to number strings and lists, among other things.

What is Python enumerate()?

The Python function enumerate() is used to turn an input into an enumeration. The objects, which can also be strings or Python tuples, for example, are each assigned a counter. The enumeration is consecutive and starts at “0” by default. The function is included in the programming language by default.

What is the syntax and parameter of Python enumerate()?

The syntax of Python enumerate() looks like this:

enumerate(iterable, start)
python

It has two parameters:

  • iterable: “iterable” is an object or a sequence that can be executed in a loop. This parameter is placed in front of the enumeration later on. It is mandatory.
  • start: This is an optional parameter. You can use it to determine from which numerical value the numbering should start. Its default value is “0”.

Example of an enumeration with enumerate()

To illustrate how this works, let’s choose a simple example with four different colors. We number these using Python enumerate(). The corresponding code looks like this:

colors = ['Blue', 'Red', 'Yellow', 'Orange']
sequence = enumerate(colors)
print(list(sequence))
python

This is how we get this output:

[(0, 'Blue'), (1, 'Red'), (2, 'Yellow'), (3, 'Orange')]
python

Python enumerate() with a start index

As we have not specified a start index, the enumeration starts at “0”. To change this, we add the parameter “start” with the value “1” to Python enumerate(). To do this, we slightly change the code from above:

colors = ['Blue', 'Red', 'Yellow', 'Orange']
sequence = enumerate(colors, 1)
print(list(sequence))
python

This makes the output look a little nicer:

[(1, 'Blue'), (2, 'Red'), (3, 'Yellow'), (4, 'Orange')]
python

However, you can adjust the start index as you wish so that the enumeration can also begin with any other value.

Enumerate() function with a for loop

In combination with a for loop, we can combine the use of Python enumerate() with and without start index. For the first loop, we don’t need the “start” parameter. The count therefore starts at “0”. For the second loop, we include the parameter with the starting point “5”. Therefore, this count starts from here. With the "\n" label, we instruct the system to create a new line each time so that it is a little clearer. This is the code for this combination:

colors = ['Blue', 'Red', 'Yellow', 'Orange']
for sequence in enumerate(colors):
    print(sequence)
    print("\n")
print("It continues from 5")        
for sequence in enumerate(colors, 5):
    print(sequence)
    print("\n")
python

Our output now looks like this:

(0, 'Blue')
(1, 'Red')
(2, 'Yellow')
(3, 'Orange')
It continues from 5
(5, 'Blue')
(6, 'Red')
(7, 'Yellow')
(8, 'Orange')
python

How to enumerate strings with Python enumerate()

As the name suggests, Python enumerate() is also the right choice if you want to enumerate a string. This breaks down the string into its individual parts and numbers them. This is what the code looks like:

string = "example"
for x in enumerate(string, 1):
    print(x)
python

This is the right output:

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

The best solution for websites and apps: With Deploy Now from IONOS, you can deploy your web projects directly via GitHub. Not only do you benefit from fair pricing, but you can also tailor the setup perfectly to your needs. Our experts are waiting to advise you now!

Was this article helpful?
Go to Main Menu