I have a dictionary representing a folder tree:

```
folders = [{
    "NAME": " Folder 1",
    "ID": "869276"
}, {
    "ID": "869277",
    "NAME": "- Sub-folder 1"
}, {
    "ID": "869279",
    "NAME": "-- Sub-sub-folder 1"
}, {
    "NAME": "--- Sub-sub-folder 1 2",
    "ID": "869285"
}, {
    "NAME": "--- Sub-sub-folder 1 3",
    "ID": "869286"
}, {
    "NAME": "-- Sub-sub-folder 2",
    "ID": "869280"
}, {
    "ID": "869281",
    "NAME": " Folder 2"
}, {
    "ID": "869282",
    "NAME": "- Sub-folder 2"
}, {
    "NAME": "- Sub-folder 2 1",
    "ID": "869283"
}, {
    "NAME": "-- Sub-Sub-folder 2 1",
    "ID": "869284"
}]

```

More clear representation:

```
 Folder 1
- Sub-folder 1
-- Sub-sub-folder 1
--- Sub-sub-folder 1 2
--- Sub-sub-folder 1 3
-- Sub-sub-folder 2
 Folder 2
- Sub-folder 2
- Sub-folder 2 1
-- Sub-Sub-folder 2 1

```

I need to organize this dictionary into a new dictionary where every folder has the value for the parent folder, like

```
 [{
    "NAME": " Folder 1",
    "ID": "869276",
    "PARENT": "0"
}, {
    "ID": "869277",
    "NAME": "- Sub-folder 1",
    "PARENT": "869276"
}, 
...
]

```

So what I thought is to count the number of '-' before the folder name to keep track of folder depth:

```
for folder in folders:
    # Folders in root have a whitespace before the name
    depth = folder['NAME'].split(' ')[0].count('-')
    if depth == 0:
        parent = '0'
    else:
        #for each previous_folder:
            previous_depth = previous_folder['NAME'].split(' ')[0].count('-')
            if previous_depth < depth:
                 parent = prvious_folder['ID']
            else:
                 #keep looking...

```

The problem is filling the commented lines with actual working code. How can I interact with each previous folder on the list starting from the current one? And how I keep looping?