Let me elaborate on why I love Python enumerate! It adds a counter to your for loop without you having to explicitly update the count/index each time. Which can save you a lot of time – and prevent a lot of silly coding errors!
Basically, a “for loop” is a coding thing where you tell the computer to go through some list of things and do something each time, such as printing out the thing.
things = ["pipet", "centrifuge", "beaker", "flask"]
for thing in things:
print(thing)
pipet
centrifuge
beaker
flaskAnd with enumerate it will keep count as it goes. You can access that count by doing something like this:
for i, thing in things:
“i” will be the index and each time you go through, it will be updated
things = ["pipet", "centrifuge", "beaker", "flask"]
for thing in enumerate(things):
print(thing)
(0, 'pipet')
(1, 'centrifuge')
(2, 'beaker')
(3, 'flask')
for i, thing in enumerate(things):
print(i)
print(thing)
0
pipet
1
centrifuge
2
beaker
3
flask
Like most things in Python, it will start at 0 by default (so i=0 for the first thing in the list). But you can specify the start value (just put “, start = “ to get it to start wherever you want).
for i, thing in enumerate(things, start=1):
print(i)
print(thing)
1
pipet
2
centrifuge
3
beaker
4
flask
I think it’s easiest to explain with examples, so hope this quick vid and below examples help. I will also upload the code file if you want to look at the syntax and play around.
python file: https://drive.google.com/file/d/10yJ0Zbd7YER73zec6nAZ8v244gzXv-Pp/view?usp=drive_link
Jupiter notebook: https://drive.google.com/file/d/1dS4i4MH_IPJzfIQvhaE9auJvcWRSlz8h/view?usp=drive_link
I’m definitely not a Python expert, but this is one of those fundamental Python functions that anyone who only tangentially uses Python should know. And for some reason it took me a while to find out about it. So I thought I’d introduce you if you weren’t familiar with it.
If you want more information, RealPython has a nice guide:
Python enumerate(): Simplify Looping With Counters by Bryan Weber https://realpython.com/python-enumerate/
And here’s the official documentation for it: https://docs.python.org/3/library/functions.html#enumerate
It’s a built-in Python function so you don’t have to import anything. Just enjoy!
things = ["pipet", "centrifuge", "beaker", "flask"]
for thing in things:
print(thing)
pipet
centrifuge
beaker
flask
for thing in enumerate(things):
print(thing)
(0, 'pipet')
(1, 'centrifuge')
(2, 'beaker')
(3, 'flask')
for i, thing in enumerate(things):
print(i)
print(thing)
0
pipet
1
centrifuge
2
beaker
3
flask
#much simpler than other ways to get an index
#initialize count
i=0
for thing in things:
print(i)
print(thing)
#advance index
i+=1
0
pipet
1
centrifuge
2
beaker
3
flask
for i, thing in enumerate(things):
if i==0:
print("The first thing is "+thing)
elif i==2:
print("The third thing is "+thing)
else:
print(thing)
The first thing is pipet
centrifuge
The third thing is beaker
flask
for i, thing in enumerate(things, start=1):
print(i)
print(thing)
1
pipet
2
centrifuge
3
beaker
4
flask
for i, thing in enumerate(things, start=1):
if i==0:
print("The first thing is "+thing)
elif i==2:
print("The third thing is "+thing)
else:
print(thing)
pipet
The third thing is centrifuge
beaker
flask
for i, thing in enumerate(things, start=1):
if i==1:
print("The first thing is "+thing)
elif i==3:
print("The third thing is "+thing)
else:
print(thing)
The first thing is pipet
centrifuge
The third thing is beaker
flaskmore coding-related videos: https://www.youtube.com/playlist?list=PLUWsCDtjESrHbBVTt6wqWBGGbd0Q9pbRA
more Python tips: https://bit.ly/bb_python_tips
more on why you should learn to code: https://bit.ly/why_program


