How to number and output objects using Python enumerate()
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.
- Industry-leading security
- Communication and collaboration tools
- Hosted and developed in Europe
What is the syntax and parameter of Python enumerate()
?
The syntax of Python enumerate()
looks like this:
enumerate(iterable, start)
pythonIt 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 colours. We number these using Python enumerate()
. The corresponding code looks like this:
colours = ['Blue', 'Red', 'Yellow', 'Orange']
sequence = enumerate(colours)
print(list(sequence))
pythonThis is how we get this output:
[(0, 'Blue'), (1, 'Red'), (2, 'Yellow'), (3, 'Orange')]
pythonPython 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:
colours = ['Blue', 'Red', 'Yellow', 'Orange']
sequence = enumerate(colours, 1)
print(list(sequence))
pythonThis makes the output look a little nicer:
[(1, 'Blue'), (2, 'Red'), (3, 'Yellow'), (4, 'Orange')]
pythonHowever, 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:
colours = ['Blue', 'Red', 'Yellow', 'Orange']
for sequence in enumerate(colours):
print(sequence)
print("\n")
print("It continues from 5")
for sequence in enumerate(colours, 5):
print(sequence)
print("\n")
pythonOur 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')
pythonHow 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)
pythonThis is the right output:
(1, 'e')
(2, 'x')
(3, 'a')
(4, 'm')
(5, 'p')
(6, 'l')
(7, 'e')
pythonThe 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!