1

This seems pretty simple, but I haven't found a way to do it. I have three lists, a, b, and c. I want to iterate over all of them as if their elements altogether form one big list. I can think of a few ways to do this, but nothing very smooth or "pythonic". I expected the splat operator to work:

for e in (*a, *b, *c):
   # do stuff with e

but that gives a syntax error. Any ideas?

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115

1 Answers1

9

If they are all lists, all tuples, or all strings, then you can concatenate them:

for e in a + b + c:

For any combination of any iterables, including iterators like generators:

from itertools import chain

for e in chain(a, b, c):
Alex Hall
  • 34,833
  • 5
  • 57
  • 89