Spread the love
Sometimes you may want to loop over multiple lists or other sequences at the same time. You sure can do it in Python. Here you should make use of the zip function. First let’s define three sequences, for instance lists, and then let’s loop over all three lists simultaneously to print out a formatted string containing built-in elements coming from the lists. Here’s the code. Watch how the zip function is used:
words_EN = ["cat", "dog", "tree"]
words_DE = ["Katze", "Hund", "Baum"]
words_FR = ["chat", "chien", "arbre"]
for en, de, fr in zip(words_EN, words_DE, words_FR):
print(f"English: {en:10} German: {de:10} French: {fr}")
The output is:
English: cat German: Katze French: chat
English: dog German: Hund French: chien
English: tree German: Baum French: arbre
Here’s the video version:
Spread the love