zardav
zardav

Reputation: 1248

Iterating over two lists one after another

I have two lists list1 and list2 of numbers, and I want to iterate over them with the same instructions. Like this:

for item in list1:
  print(item.amount)
  print(item.total_amount)

for item in list2:
  print(item.amount)
  print(item.total_amount)

But that feels redundant. I know I can write for item in list1 + list2:, but it has a price of running-time.

Is there a way do that without losing time?

Upvotes: 35

Views: 27017

Answers (4)

Carlos Mosquera
Carlos Mosquera

Reputation: 41

After several executions, the fastest alternative was the second one.

import itertools
from time import time

l1 = list(range(1000))
l2 = list(range(1000))

print('Alternative 1. Itertools')
t = time()
for j in range(10000):
    s = 0
    for i in itertools.chain(l1, l2):
        s += i
print(time() - t)

print('Alternative 2. Naive')
t = time()
for j in range(10000):
    s = 0
    for i in [*l1, *l2]:
        s += i
print(time() - t)

print('Alternative 3. Yield')
def chain(*ls):
    for l in ls:
        for k in l:
            yield k
t = time()
for j in range(10000):
    s = 0
    for i in chain(l1, l2):
        s += i
print(time() - t)

Upvotes: 0

Christian Gao
Christian Gao

Reputation: 7

How about this:

for item in list1 + list2:
    print(item.amount)
    print(item.total_amount)

Only 3 lines

Upvotes: -2

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160377

This can be done with itertools.chain:

import itertools

l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]

for i in itertools.chain(l1, l2):
    print(i, end=" ")

Which will print:

1 2 3 4 5 6 7 8 

As per the documentation, chain does the following:

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.

If you have your lists in a list, itertools.chain.from_iterable is available:

l = [l1, l2]
for i in itertools.chain.from_iterable(l):
    print(i, end=" ")

Which yields the same result.

If you don't want to import a module for this, writing a function for it is pretty straight-forward:

def custom_chain(*it):
    for iterab in it:
        yield from iterab

This requires Python 3, for Python 2, just yield them back using a loop:

def custom_chain(*it):
    for iterab in it:
        for val in iterab:
            yield val

In addition to the previous, Python 3.5 with its extended unpacking generalizations, also allows unpacking in the list literal:

for i in [*l1, *l2]:
    print(i, end=" ")

though this is slightly faster than l1 + l2 it still constructs a list which is then tossed; only go for it as a final solution.

Upvotes: 53

Kevin
Kevin

Reputation: 76184

chain works, but if you feel that it's overkill to import a module just to call a single function once, you can replicate its behavior inline:

for seq in (list1, list2):
  for item in seq:
    print(item.amount)
    print(item.total_amount)

Creating the (list1, list2) tuple is O(1) with respect to list length, so it should perform favorably in comparison to concatenating the lists together.

Upvotes: 16

Related Questions