How to Iterate Over Multiple Lists Sequentially in Python
Do you need to process items of multiple Python lists in one go? Here are some simple ways to do this using the itertools library and for loop.
Join the DZone community and get the full member experience.
Join For FreePython list is a versatile data structure that allows you to easily store a large amount of data in a compact manner. Lists are widely used by Python developers and support many useful functions out-of-the-box. Often you may need to work with multiple lists or a list of lists and iterate over them sequentially, one after another. There are several simple ways to do this. In this article, we will learn how to go through multiple Python lists in a sequential manner.
Let us say you have the following 3 lists.
L1=[1,2,3]
L2=[4,5,6]
L3=[7,8,9]
1. Using itertools.chain()
itertools
is a very useful Python library that provides many functions to easily work with iterable data structures such as list. You can use the itertools.chain()
function to quickly go through multiple lists sequentially. Here is an example of iterating through lists L1, L2, and L3 using the chain()
function.
>>> for i in itertools.chain(L1,L2,L3):
print i
1
2
3
4
5
6
7
8
9
Using itertools
is one of the fastest and most memory-efficient ways to go through multiple lists since it uses iterators. This is because iterators only return one item at a time, instead of storing a copy of the entire iterable in memory, as is the case of for loop.
2. Using for Loop
Sometimes you may have a list of lists, as shown below.
L4 = [L1, L2, L3]
print L4
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In such cases, you can use a nested for
loop to iterate through these lists.
>>> for i in L4:
for j in i:
print j
1
2
3
4
5
6
7
8
9
Alternatively, you can also use itertools.chain()
to go through a list of lists.
>>> for i in itertools.chain(L4):
for j in i:
print j
1
2
3
4
5
6
7
8
9
3. Using Star Operator
The above-mentioned methods work with most Python versions. But if you use Python 3+, then you can also avail the star (*
) operator to quickly unpack a list of lists.
for i in [*L1, *L2, *L3]:
print(i)
1
2
3
4
5
6
7
8
9
4. Using itertools.izip()
So far, in each of the above cases, all items of the first list are displayed, followed by all items of the second list, and so on. But sometimes you may need to sequentially process the first item of each list, followed by the second item of each list, and so on. For this kind of sequential order, you need to use the itertools.izip()
function. Here is an example to illustrate it.
for i in itertools.izip(*L4):
for j in i:
print j
1
4
7
2
5
8
3
6
9
Notice the difference in sequence. In this case, the output is the first item of each list (1
, 4
, 7
), followed by the second item on each list (2
, 5
, 8
), and so on. This is different from the sequence of the first list items (1
, 2
, 3
) followed by second list items (4
, 5
, 6
), and so on.
Conclusion
In this article, we have learned several simple ways to sequentially iterate over multiple lists in Python. Basically, there are two ways to do this. The first approach is when you need to process all items of one list before moving to the next one. The second approach is where you need to process the first item of each list then the second item of each list and so on. In the first case, you can use the itertools.chain()
function, a for
loop, or a star(*
) operator. In the second case, you need to use the itertools.izip()
function.
Opinions expressed by DZone contributors are their own.
Comments