2

There are already many answers to 'how to iterate through 2 lists in parallel'. However, I am trying to figure out how to iterate through an arbitrary amount of lists.

For example, the following requires me to know beforehand how many lists i have

for f, b in zip(foo, bar):
    print(f, b)

But I am using os.listdir() to get an N amount of subfolders, and then I want to iterate through all those subfolder in parallel.

for (every nth element) in (each subfolder found):
    do something

Currently I have always done this manually but I'd really like to know how to do this more elegantly, i.e. without any knowledge of how many lists to iterate through.

Thanks!

4 Answers 4

4

You can still use zip, since it accepts more than two arguments. Given a list of lists, where you want to iterate through their i'th arguments in a loop:

lists = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
for parallel in zip(*lists):
    print(parallel)

prints:

(1, 1, 1)
(2, 2, 2)
(3, 3, 3)
(4, 4, 4)
(5, 5, 5)

Or, with a variant of your problem description:

import os

subdirs = [os.listdir(f) for f in os.listdir(my_dir) if os.path.isdir(os.path.join(my_dir, f))]
for parallel in zip(*subdirs):
    # work on them in parallel

It should be noted that zip cuts off on the end of the shortest list it gets. If you want to pad every list with Nones instead, you should use itertools.zip_longest, like so:

from itertools import zip_longest
# get subdirs same as before
for parallel in zip_longest(*subdirs):
    print([f for f in parallel if f is not None])  # don't show padded entries
Sign up to request clarification or add additional context in comments.

Comments

2

You could just unpack the list of lists:

list_of_lists = [[1,2,3], [2,3,4], [5,6,7]]

for x in zip(*list_of_lists):
    for y in x:
        print(y)
1
2
4
2
3
5
3
4
6

Comments

1

You don't need to know how many lists you'll have in advance:

for subdirs in zip(*(os.listdir(p) for p in root_dirs)):

for paths in zip(*(os.listdir(p) for p in root_dirs))::
    do_something(paths)  # paths is a tuple like (paths[0], paths[1], ...)

Comments

0

for your list of subfolders:

subfolders = os.listdir(some_path)

you can create a second for loop around your first one:

# iterate through subfolders of some_path
for subfolder in subfolders:
    #iterate through elements in subfolder
    for element in os.listdir(os.path.join(some_path, subfolder):
        # you take it from here

You might also want to take a look at os.walk to recursively iterate through a directory tree

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.