avatarOliver S

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1981

Abstract

6"><pre>numbers = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] latin_letters = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>] greek_letters = [<span class="hljs-string">"alpha"</span>, <span class="hljs-string">"beta"</span>, <span class="hljs-string">"gamma"</span>]

<span class="hljs-keyword">for</span> number, latin_letter, greek_letter <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(numbers, latin_letters, greek_letters): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Latin letter <span class="hljs-subst">{number}</span>: <span class="hljs-subst">{latin_letter}</span>, greek letter <span class="hljs-subst">{number}</span>: <span class="hljs-subst">{greek_letter}</span>"</span>)</pre></div><h1 id="23ca">Combing Enumerate and Zip</h1><p id="27ad">We can further shorten above code by integrating the previously introduced <code>enumerate()</code> function:</p><div id="7b70"><pre>latin_letters = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>] greek_letters = [<span class="hljs-string">"alpha"</span>, <span class="hljs-string">"beta"</span>, <span class="hljs-string">"gamma"</span>]

<span class="hljs-keyword">for</span> number, (latin_letter, greek_letter) <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(<span class="hljs-built_in">zip</span>(latin_letters, greek_letters)): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Latin letter <span class="hljs-subst">{number}</span>: <span class="hljs-subst">{latin_letter}</span>, greek letter <span class="hljs-subst">{number}</span>: <span class="hljs-subst">{greek_letter}</span>"</span>)</pre></div><p id="06d5">This post is part of a series show-casing important Py

Options

thon concepts quickly. You can find the other parts here:</p><ul><li>Part 1: <a href="https://readmedium.com/lambda-functions-in-python-a124cebc2e99">Lambda Functions in Python</a></li><li>Part 2: <a href="https://readmedium.com/iterators-in-python-bd7332ddbc00">Iterators in Python</a></li><li>Part 3: <a href="https://readmedium.com/generators-and-generator-expressions-in-python-a8d2e700945e">Generators and Generator Expressions in Python</a></li><li>Part 5: <a href="https://readmedium.com/managing-resources-in-python-with-context-managers-with-statement-f07afc1afb4f">Managing Resources in Python with Context Managers (with statement)</a></li><li>Part 6: <a href="https://readmedium.com/generating-temporary-files-and-directories-in-python-dfc11f017a97">Generating Temporary Files and Directories in Python</a></li><li>Part 7: <a href="https://readmedium.com/logging-in-python-3df84ce78cef">Logging in Python</a></li><li>Part 8: <a href="https://readmedium.com/partial-functions-in-python-66998eef1384">Partial Functions in Python</a></li><li>Part 9: <a href="https://readmedium.com/f-strings-in-python-80e30c64fb95">f-Strings in Python</a></li></ul><h1 id="bdb4">Level Up Coding</h1><p id="7a18">Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>💰 Free coding interview course ⇒ <a href="https://skilled.dev/?utm_source=luc&amp;utm_medium=article">View Course</a></li><li>🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev">Newsletter</a></li></ul><p id="643a">🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><b>Join the Level Up talent collective and find an amazing job</b></a></p></article></body>

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

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

Python
Python Iterator
Python Programming
Recommended from ReadMedium