I have list containing some paths:
['folder1/folder2/Module1', 'folder4/folder5/Module2', 'folder7/folder8/Module3', 'folder12/folder13/Module4', 'folder17/folder20/folder50/Module5' .. etc]
What would be the best way to extract each element of that list and create new list or some other place to store that path with it's specific name?
Mu current code for going through each element of the list and storing it one by one, but I can't generate new list for each element, not sure if that is even possible:
for j in range(len(listOfPaths)):
del pathList[:]
path = listOfPaths[j]
pathList.append(path)
So to clarify, at the end what I need is to get one list list[Module1] that contains only 'folder1/folder2/Module1', and second one list[Module2] with only path to Module2, etc...
It might be better to use a Dictionary here, instead of a list.
#!/usr/bin/env python3
import os
paths = []
paths.append("folder1/subfolderA/Module1")
paths.append("folder2/subfolderB/Module1")
paths.append("folder3/subfolderC/Module1")
paths.append("folder4/subfolderD/Module2")
paths.append("folder5/subfolderE/Module2")
paths.append("folder6/subfolderF/Module50")
# create an empty dictionary
modulesDict = {}
# it will look like this:
# "ModuleX" -> ["path1/to/ModuleX", "path2/to/ModuleX", ...]
# "ModuleY" -> ["path3/to/ModuleY", "path4/to/ModuleY", ...]
for path in paths: # loop over original list of paths
# take only the "ModuleX" part
moduleName = os.path.basename(os.path.normpath(path))
# check if its already in our dict or not
if moduleName in modulesDict:
# add the path to the list of paths for that module
modulesDict.get(moduleName).append(path)
else:
# create an new list, with only one element (only the path)
modulesDict[moduleName] = [path]
print(modulesDict)
OUTPUT: (formatted a bit)
{
'Module1':
['folder1/subfolderA/Module1', 'folder2/subfolderB/Module1', 'folder3/subfolderC/Module1'],
'Module2':
['folder4/subfolderD/Module2', 'folder5/subfolderE/Module2'],
'Module50':
['folder6/subfolderF/Module50']
}