Originally posted on tomshardware.
Enumerating in Python is as easy as 1, 2, 3! Or should we say 0, 1, 2?
Once you’ve created a list or collection in Python, it might be useful to have each item numbered. Instead of going through the list manually and adding numerals one by one, it can be well worth looking into the enumerate function. This is a built-in tool that will go through a list and automatically number them in the order they were added to the tuple or list.
In this guide, we’ll go over how to use this enumerate function with an existing list. We’ll assume you already have familiarity with how to create a variable, list, and use functions. Once the list has been enumerated, it can be called later with the enumeration in place. You can even use a for loop (see how to use for loops in Python) to iterate through the enumeration.
What is Enumeration in Python?
Enumeration is the ordered listing of all items in a collection. In Python we can use this with a list or tuple to (data collection objects) into an enumerate object. The enumerate object has a name and value for each enum member within the object. The enumerate object can be updated (mutable) unlike other objects such as tuples which are immutable (cannot be updated).
How to Enumerate in Python
To demonstrate how to use enumeration in Python, we’ll start with a list of flavors and label it ‘flavors’. After applying the enumeration function, we’ll print the list which will show that each item has been marked with a numeral sequentially.
1. Create a list, collection or tuple. In this case we have three flavors in a list labeled as a variable named ‘flavors’.
flavors = ('Raspberry', 'Vanilla', 'Chocolate')
2. Run the enumerate function. In the example code below, we’ve created a variable named ‘choices’ that consists of the enumerate function parsing the ’flavors’ object, as an argument.
flavors = ('Raspberry', 'Vanilla', 'Chocolate') choices = enumerate(flavors)
3. Print the list. You can now call the ‘flavors’ object as needed in your code. For the sake of our example, we’ll print the list to show what it looks like when the list has been enumerated.
flavors = ('Raspberry', 'Vanilla', 'Chocolate') choices = enumerate(flavors) print(list(choices))
4. Confirm the results. The printed list should appear as follows if the enumeration was successful.
[(0, 'Raspberry'), (1, 'Vanilla'), (2, 'Chocolate')]
Even though we’ve printed the list to show the enumerate function, this is just to demonstrate how it works. You can apply it in your code as needed after the second step but printing the list is a quick way to make sure it’s working as intended.
We earlier mentioned that enumerated collections have a value and name associated to the data stored within. We can individually extract these values. Here is a quick example that creates an enumerated collection of foods.
1. Import the enum module.
2. Create a new enumeration object using a function-call syntax. Our object is called food, and in there we store a list of foods. By using the enum.Enum function call we bind values to each of the item names.
food = enum.Enum('food', ['Tomato', 'Cucumber', 'Chocolate'])
3. Use a for loop to print the value and name of each item in the object. The for loop will iterate over every item in the object. We then print the value and name of the item.
for item in food: print(item.value, item.name)
4. Run the code. The output will be the enumerated value of the item’s position and the name of the item.
1 Tomato 2 Cucumber 3 Chocolate
Using Enumeration to Number Chapters in a Book
There are plenty of practical applications for applying numbers to a list. In this example, we’ll number the chapters in a book we’re pretending to write. This example also shows how you can start a list from any number so it doesn’t begin with the default value of 0.
I’ll be using Thonny for this example. It’s a free Python editor that works on several platforms. You can find it available for download on the official Thonny website. However, you can use any editor you like for your project.
1. Make your list. In this case, my list is a handful of fake chapters for our book.
chapters = ["How to Enumerate in Python", "Creating a List", "Enumerating the List"]
2. Set the beginning of the list to 1 instead of 0. The next line tells Python that we want the chapter enumeration to begin at 1. The words ‘chapter’ and ‘title’ can be anything you like.
chapters = ["How to Enumerate in Python", "Creating a List", "Enumerating the List"] for chapter, title in enumerate (chapters,1):
3. Print the list. The third line prints the chapter variable which has the title argument setting the list to begin at 1.
chapters = ["How to Enumerate in Python", "Creating a List", "Enumerating the List"] for chapter, title in enumerate (chapters,1): print(chapter, title)
4. Run the program. It should print the chapters with the enumeration beginning at 1.