Map three lists

I have following three lists:

paths = ["c:/path/path", "d:/path/path"]
folder_one = ["fol1", "fol2"]
folder_two = ["folder1", "folder2"]

How can I map these three lists so the output could look like this:

("c:/path/path", "fol1")
("c:/path/path", "fol2")
("d:/path/path", "folder1")
("d:/path/path", "folder2")

So far I have:

somelists = [paths] + [folder_one + folder_two]
for element in itertools.product(*somelists):
    print(element)

But it also generate tuple like: ("c:/path/path", "folder1")

Can anyone give me a hint?

>Solution :

This should give what you want:

[f"{p}/{f}" for p, file in zip(paths, (folder_one, folder_two)) for f in file]
>>> ['c:/path/path/fol1',
 'c:/path/path/fol2',
 'd:/path/path/folder1',
 'd:/path/path/folder2']

You can split it up in the following parts:

zip(paths, (folder_one, folder_two))

connect each path to a list of folders (folder_one, folder_two).

Then going over each path and the files in a list:

for p, file in zip(paths, (folder_one, folder_two))

file is here a list with files.

The last part is iterating over each file in the file list:

for f in file

EDIT

Sorry I taught that you want the paths, changed for the desired output:

[(p,f) for p, file in zip(paths, (folder_one, folder_two)) for f in file]
>>> [('c:/path/path', 'fol1'),
 ('c:/path/path', 'fol2'),
 ('d:/path/path', 'folder1'),
 ('d:/path/path', 'folder2')]

Leave a Reply