Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading