Python: Use Enumerate Command - Here's how
The Enumerate command is very useful when programming with Python. You can use it to number objects in a list. Here we explain in detail how you do this.
Use the Enumerate command in Python
The Enumerate command is already integrated in Python and therefore you do not have to import it manually. What you need for this is a self-made list.
- First you have to create this list, for example: "Seasons = ['Spring', 'Summer', 'Autumn', 'Winter']"
- With the command "list (enumerate (seasons))" you can now output this list numbered.
- At this point, the program would output "[(0, 'Spring'), (1, 'Summer'), (2, 'Autumn'), (3, 'Winter')]".
- However, in order for counting to start at 1, you have to modify the Enumerate command somewhat: "list (enumerate (seasons, start = 1))"
- The program would now start counting at 1: "[(1, 'Spring'), (2, 'Summer'), (3, 'Autumn'), (4, 'Winter')]"
- Don't forget, however, that the basic enumerate command actually is "enumerate ()". In the example above, however, we combined this with a list command so that the data is output more clearly.
More tips about Python in our CHIP guides
Tip: You can find further programming tutorials on our Python programming language topic page. There we show you, for example, how you can use the copy file command in Python.