The enumerate() function in Python loops over a tuple, list, or other container object returning a counter/index and value for each element of the object. For example:
pets = ('dog', 'cat', 'parrot', 'hamster', 'mongoose')
for index, pet in enumerate(pets):
print(index, pet)
produces:
0 dog
1 cat
2 parrot
3 hamster
4 mongoose
The enumerate() function defaults to starting the index or counter at zero. There is an optional start argument to start the index or counter at another value. For example:
for index, pet in enumerate(pets, 100):
print(index, pet)
produces:
100 dog
101 cat
102 parrot
103 hamster
104 mongoose
The enumerate() function is a built-in replacement for using a manual index or counter, the common practice in C-style languages such as C, C++, and Java. For example:
index = 0 # manual index
for pet in pets:
print(index, pet)
index += 1
which also produces:
0 dog
1 cat
2 parrot
3 hamster
4 mongoose
A manual index or counter is error-prone, often resulting in off-by-one or other errors. Using the enumerate() function instead of a manual index is generally considered the “Pythonic” way to generate a counter or index during a loop over a tuple, list or other container.
The Python programming language community uses “Pythonic” in a non-standard English way that may mean:
“cool” as in James Dean was cool
“Exploiting the features of the Python language to produce code that is clear, concise, and maintainable.” https://stackoverflow.com/questions/25011078/what-does-pythonic-mean
Many other definitions of Pythonic exist.
Pythonic is also a rarely used adjective in standard English meaning:
(1) of or relating to pythons (the snake)
(2) similar to a python; pythonlike
(3) gigantic or monstrous
enumerate() and Dictionaries
Like most modern programming languages, Python has a built-in dictionary type, also known as an associative array or hash table in other languages. Technically, a hash table is a method to implement a dictionary/associative array, but is often used as a synonym for a dictionary or associative array.
In Python, dictionaries take a key, an object, and return an associated value object. Very often the key and value are strings, but they need not be. For example, this dictionary enables one to look up the type of pet from the pet’s name:
pets_dict = { "Toby" : "dog", "Serena" : "cat", "Ted" : "hamster", "Wilma" : "parrot", "Riki-tiki-tavi" : "mongoose" }
By default, the for loop in Python loops over the keys in the dictionary. For example:
for key in pets_dict:
print(key)
produces:
Toby
Serena
Ted
Wilma
Riki-tiki-tavi
on my computer. The order of the keys is not guaranteed.
for key in pets_dict:
print(key, "is a", pets_dict[key])
produces:
Toby is a dog
Serena is a cat
Ted is a hamster
Wilma is a parrot
Riki-tiki-tavi is a mongoose
pets_dict.keys() returns a dict_keys container with the list of keys in the dictionary.
pets_dict.values() returns a dict_values container with the list of values in the dictionary.
pets_dict.items() returns a dict_items container with the list of (key, value) tuples in the dictionary.
What does enumerate() do when applied to a dictionary?
By default, Python enumerate() is applied to the keys in the dictionary. Remember the for loop in Python defaults to looping over the keys of a dictionary. For example:
for index, value in enumerate(pets_dict):
print(index, value)
produces:
0 Toby
1 Serena
2 Ted
3 Wilma
4 Riki-tiki-tavi
Toby, Serena, and so on are the keys of the dictionary, the names of the pets in the example.
In some cases, you may need an index into the list of tuples of items in the dictionary:
for index, item in enumerate(pets_dict.items()):
print(index, item)
produces:
0 ('Toby', 'dog')
1 ('Serena', 'cat')
2 ('Ted', 'hamster')
3 ('Wilma', 'parrot')
4 ('Riki-tiki-tavi', 'mongoose')
(C) 2019 by John F. McGowan, Ph.D.
About Me
John F. McGowan, Ph.D. solves problems using mathematics and mathematical software, including developing gesture recognition for touch devices, video compression and speech recognition technologies. He has extensive experience developing software in C, C++, MATLAB, Python, Visual Basic and many other programming languages. He has been a Visiting Scholar at HP Labs developing computer vision algorithms and software for mobile devices. He has worked as a contractor at NASA Ames Research Center involved in the research and development of image and video processing algorithms and technology. He has published articles on the origin and evolution of life, the exploration of Mars (anticipating the discovery of methane on Mars), and cheap access to space. He has a Ph.D. in physics from the University of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Technology (Caltech).