I want to change the following code
for directory, dirs, files in os.walk(directory_1):
do_something()
for directory, dirs, files in os.walk(directory_2
I think itertools.chain() should do it.
from itertools import chain
x = iter([1,2,3]) #Create Generator Object (listiterator)
y = iter([3,4,5]) #another one
result = chain(x, y) #Chained x and y
All those complicated solutions...
just do:
for dir in directory_1, directory_2:
for directory, dirs, files in os.walk(dir):
do_something()
If you really want to "join" both generators, then do :
for directory, dirs, files in [
x for osw in [os.walk(directory_1), os.walk(directory_2)]
for x in osw
]:
do_something()
With itertools.chain.from_iterable you can do things like:
def genny(start):
for x in range(start, start+3):
yield x
y = [1, 2]
ab = [o for o in itertools.chain.from_iterable(genny(x) for x in y)]
print(ab)
2020 update: Work in both python 3 and python 2
import itertools
iterA = range(10,15)
iterB = range(15,20)
iterC = range(20,25)
### first option
for i in itertools.chain(iterA, iterB, iterC):
print(i)
# 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
### alternative option, introduced in python 2.6
for i in itertools.chain.from_iterable( [iterA, iterB, iterC] ):
print(i)
# 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
itertools.chain() is the basic.
itertools.chain.from_iterables is handy if you have an iterable of iterables. For example a list of files per subdirectory like [ ["src/server.py", "src/readme.txt"], ["test/test.py"] ]
.
One can also use unpack operator *
:
concat = (*gen1(), *gen2())
NOTE: Works most efficiently for 'non-lazy' iterables. Can also be used with different kind of comprehensions. Preferred way for generator concat would be from the answer from @Uduse