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:
- Part 1: Lambda Functions in Python
- Part 2: Iterators in Python
- Part 3: Generators and Generator Expressions in Python
- Part 5: Managing Resources in Python with Context Managers (with statement)
- Part 6: Generating Temporary Files and Directories in Python
- Part 7: Logging in Python
- Part 8: Partial Functions in Python
- Part 9: f-Strings in Python
Level Up Coding
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Level Up Coding publication
- 💰 Free coding interview course ⇒ View Course
- 🔔 Follow us: Twitter | LinkedIn | Newsletter
🚀👉 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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.