Can you loop through multiple lists in python?

Looping Through Multiple Lists

Credit: Andy McKay

Problem

You need to loop through every item of multiple lists.

Solution

There are basically three approaches. Say you have:

a = ['a1', 'a2', 'a3']
b = ['b1', 'b2']

Using the built-in function map, with a first argument of None, you can iterate on both lists in parallel:

print "Map:"
for x, y in map[None, a, b]:
    print x, y

The loop runs three times. On the last iteration, y will be None.

Using the built-in function zip also lets you iterate in parallel:

print "Zip:"
for x, y in zip[a, b]:
    print x, y

The loop runs two times; the third iteration simply is not done.

A list comprehension affords a very different iteration:

print "List comprehension:"
for x, y in [[x,y] for x in a for y in b]:
    print x, y

The loop runs six times, over each item of b for each item of a.

Discussion

Using map with None as the first argument is a subtle variation of the standard map call, which typically takes a function as the first argument. As the documentation indicates, if the first argument is None, the identity function is used as the function through which the arguments are mapped. If there are multiple list arguments, map returns a list consisting of tuples that contain the corresponding items from all lists [in other words, it’s a kind of transpose operation]. The list arguments may be any kind of sequence, and the result is always a list.

Note that the first technique returns None for sequences in which there are no more elements. Therefore, the output of the first loop is:

Map:
a1 b1
a2 b2
a3 None

zip lets you iterate over the lists in a similar way, but only up to the number of elements of the smallest list. Therefore, the output of the second technique is:

Zip:
a1 b1
a2 b2

Python 2.0 introduced list comprehensions, with a syntax that some found a bit strange:

[[x,y] for x in a for y in b]

This iterates over list b for every element in a. These elements are put into a tuple [x, y]. We then iterate through the resulting list of tuples in the outermost for loop. The output of the third technique, therefore, is quite different:

List comprehension:
a1 b1
a1 b2
a2 b1
a2 b2
a3 b1
a3 b2

See Also

The Library Reference section on sequence types; documentation for the zip and map built-ins in the Library Reference.

Let's say I have two or more lists of same length. What's a good way to iterate through them?

a, b are the lists.

 for i, ele in enumerate[a]:
    print ele, b[i]

or

for i in range[len[a]]:
   print a[i], b[i]

or is there any variant I am missing?

Is there any particular advantages of using one over other?

nbro

14.3k27 gold badges104 silver badges188 bronze badges

asked Apr 9, 2012 at 21:55

0

The usual way is to use zip[]:

for x, y in zip[a, b]:
    # x is from a, y is from b

This will stop when the shorter of the two iterables a and b is exhausted. Also worth noting: itertools.izip[] [Python 2 only] and itertools.izip_longest[] [itertools.zip_longest[] in Python 3].

answered Apr 9, 2012 at 21:55

Sven MarnachSven Marnach

543k114 gold badges914 silver badges816 bronze badges

4

You can use zip:

>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> for x, y in zip[a, b]:
...   print x, y
... 
1 a
2 b
3 c

answered Apr 9, 2012 at 21:57

Can you use for loops with lists in Python?

In Python, we can loop over list elements with for and while statements, and list comprehensions.

Can you have multiple for loops in Python?

Nested For Loops Loops can be nested in Python, as they can with other programming languages. The program first encounters the outer loop, executing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion.

Can you use lists in for loops?

You can use a for loop to create a list of elements in three steps: Instantiate an empty list. Loop over an iterable or range of elements. Append each element to the end of the list.

How do you make a list of lists in Python?

In Python, we can create a list by surrounding all the elements with square brackets [] and each element separated by commas. It can be used to store integer, float, string and more.

Chủ Đề