# Map three lists

## Question:

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?

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')]
``````

You can use `zip_longest` (here)

``````list(itertools.zip_longest(paths, folder_one+folder_two))
``````

output –

`[('c:/path/path', 'fol1'), ('d:/path/path', 'fol2'), (None, 'folder1'), (None, 'folder2')]`

For your specific output format you can use the following:

``````import itertools
paths = ["c:/path/path", "d:/path/path"]
per_path_folders = [["fol1", "fol2"], ["folder1", "folder2"]]
all_paths = []
for path, folders in zip(paths, per_path_folders):
all_paths.extend(itertools.product([path], folders))

print(all_paths)
``````

Output:

[(‘c:/path/path’, ‘fol1’), (‘c:/path/path’, ‘fol2’), (‘d:/path/path’, ‘folder1’), (‘d:/path/path’, ‘folder2’)]

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.