Accessing indices in nested list

64 Views Asked by At

all_folders is nested list of interest as below:

all_folders = [['\\\\1234\\Costing\\Folder1\\', '\\\\4567\\Costing\\Folder1\\', '\\\\8910\\Costing\\Folder1\\', '\\\\1112\\Costing\\Folder1\\', '\\\\1314\\Costing\\Folder1\\', '\\\\1516\\Costing\\Folder1\\'], ['\\\\1234\\Costing\\Folder2\\', '\\\\4567\\Costing\\Folder2\\', '\\\\8910\\Costing\\Folder2\\', '\\\\1112\\Costing\\Folder2\\', '\\\\1314\\Costing\\Folder2\\', '\\\\1516\\Costing\\Folder2\\'], ['\\\\1234\\Costing\\Folder3\\', '\\\\4567\\Costing\\Folder3\\', '\\\\8910\\Costing\\Folder3\\', '\\\\1112\\Costing\\Folder3\\', '\\\\1314\\Costing\\Folder3\\', '\\\\1516\\Costing\\Folder3\\']]

Trying to access 0th, 1st, 2nd... indices from nested list above.

What I am looking for is accessing addresses like this: '\\\\1234\\Costing\\Folder1\\' '\\\\1234\\Costing\\Folder2\\', '\\\\1234\\Costing\\Folder3\\' Then move to the next index in sublists: '\\\\4567\\Costing\\Folder1\\', '\\\\4567\\Costing\\Folder2\\', '\\\\4567\\Costing\\Folder3\\'

for folders in all_folders:
    for folder in folders:
    print(folder)

This doesn't give me what I'm trying to access. I want the output as below:

\\1234\Costing\Folder1\ \\1234\Costing\Folder2\ \\1234\Costing\Folder3\ \\4567\Costing\Folder1\ \\4567\Costing\Folder2\ \\4567\Costing\Folder3\ \\8910\Costing\Folder1\ \\8910\Costing\Folder2\ \\8910\Costing\Folder3\ \\1112\Costing\Folder1\ \\1112\Costing\Folder2\ \\1112\Costing\Folder3\ \\1314\Costing\Folder1\ \\1314\Costing\Folder2\ \\1314\Costing\Folder3\ \\1516\Costing\Folder1\ \\1516\Costing\Folder2\ \\1516\Costing\Folder3\

1

There are 1 best solutions below

0
DoubleFelix On

Well, for starters, your input is invalid, as you didn't escape the backslashes that mark the end of strings.

'\\1516\Costing\Folder3\' is invalid. You need to use '\\1516\Costing\Folder3\\' instead.

Fixing that, you get this:

all_folders = [['\\1234\Costing\Folder1\\', '\\4567\Costing\Folder1\\', '\\8910\Costing\Folder1\\', '\\1112\Costing\Folder1\\', '\\1314\Costing\Folder1\\', '\\1516\Costing\Folder1\\'], ['\\1234\Costing\Folder2\\', '\\4567\Costing\Folder2\\', '\\8910\Costing\Folder2\\', '\\1112\Costing\Folder2\\', '\\1314\Costing\Folder2\\', '\\1516\Costing\Folder2\\'], ['\\1234\Costing\Folder3\\', '\\4567\Costing\Folder3\\', '\\8910\Costing\Folder3\\', '\\1112\Costing\Folder3\\', '\\1314\Costing\Folder3\\', '\\1516\Costing\Folder3\\']]

Secondly, you used All_folders. Python variables are case sensitive, so make sure you use all_folders instead.

Once you fix that too, using a nested for loop does work:

for folders in all_folders:
     for folder in folders:
             print(folder)
\1234\Costing\Folder1\
\4567\Costing\Folder1\
\8910\Costing\Folder1\
\1112\Costing\Folder1\
\1314\Costing\Folder1\
\1516\Costing\Folder1\
\1234\Costing\Folder2\
\4567\Costing\Folder2\
\8910\Costing\Folder2\
\1112\Costing\Folder2\
\1314\Costing\Folder2\
\1516\Costing\Folder2\
\1234\Costing\Folder3\
\4567\Costing\Folder3\
\8910\Costing\Folder3\
\1112\Costing\Folder3\
\1314\Costing\Folder3\
\1516\Costing\Folder3\