Skip to content
Home » How to Loop over Multiple Lists in Python

How to Loop over Multiple Lists in Python

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

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version:


Spread the love

Leave a Reply