Advanced Iteration in Python with enumerate() and zip()

Python Shorts — Part 4In this post we will take a look at some advanced iteration tools in Python — in particular enumerate() and zip().EnumerateSometimes, you might want to iterate over an iterable, accessing all its elements — while simultaneously ke…


This content originally appeared on Level Up Coding - Medium and was authored by Oliver S

Python Shorts — Part 4

In this post we will take a look at some advanced iteration tools in Python — in particular enumerate() and zip().

Enumerate

Sometimes, you might want to iterate over an iterable, accessing all its elements — while simultaneously keeping track of the iteration number, e.g.:

file_counter = 0
for file in files:
# open file ...
print(f"I have seen {file_counter + 1} files.")
file_counter += 1

Luckily, this pattern can be shortened via the enumerate() method, which adds a counter to an iterable:

for file_counter, file in enumerate(files):
# open file ...
print(f"I have seen {file_counter + 1} files.")

Zip

Another handy Python method is zip(), which pairs multiple iterators together. It will return an iterator returning over all single iterators in unison, i.e. first returning the tuple of all 1st values, then the tuple of all 2nd values etc:

numbers = [0, 1, 2, 3]
latin_letters = ["a", "b", "c"]
greek_letters = ["alpha", "beta", "gamma"]

for number, latin_letter, greek_letter in zip(numbers, latin_letters, greek_letters):
print(f"Latin letter {number}: {latin_letter}, greek letter {number}: {greek_letter}")

Combing Enumerate and Zip

We can further shorten above code by integrating the previously introduced enumerate() function:

latin_letters = ["a", "b", "c"]
greek_letters = ["alpha", "beta", "gamma"]

for number, (latin_letter, greek_letter) in enumerate(zip(latin_letters, greek_letters)):
print(f"Latin letter {number}: {latin_letter}, greek letter {number}: {greek_letter}")

This post is part of a series show-casing important Python concepts quickly. You can find the other parts here:

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job


Advanced Iteration in Python with enumerate() and zip() was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Oliver S


Print Share Comment Cite Upload Translate Updates
APA

Oliver S | Sciencx (2023-03-28T19:16:00+00:00) Advanced Iteration in Python with enumerate() and zip(). Retrieved from https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/

MLA
" » Advanced Iteration in Python with enumerate() and zip()." Oliver S | Sciencx - Tuesday March 28, 2023, https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/
HARVARD
Oliver S | Sciencx Tuesday March 28, 2023 » Advanced Iteration in Python with enumerate() and zip()., viewed ,<https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/>
VANCOUVER
Oliver S | Sciencx - » Advanced Iteration in Python with enumerate() and zip(). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/
CHICAGO
" » Advanced Iteration in Python with enumerate() and zip()." Oliver S | Sciencx - Accessed . https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/
IEEE
" » Advanced Iteration in Python with enumerate() and zip()." Oliver S | Sciencx [Online]. Available: https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/. [Accessed: ]
rf:citation
» Advanced Iteration in Python with enumerate() and zip() | Oliver S | Sciencx | https://www.scien.cx/2023/03/28/advanced-iteration-in-python-with-enumerate-and-zip/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.