0

I am working on a small project that takes a package and returns all of its dependencies to the Nth level.
Here is my current code:

def get_child(db, pkg_list):  # recursively lists all dependencies for a given package
    for pkg in pkg_list:
        if pkg in db:
            for dep in db[pkg]['Dependencies']:
                if not dep in pkg_list:  # prevents circular dependency problems
                    pkg_list.append(dep) 
                else:  # if package is already in list, need to move it to end of list
                    pkg_list.append(pkg_list.pop(pkg_list.index(dep))) # THIS IS PROBLEMATIC
    pkg_list.reverse()
    return pkg_list

This function will search the JSON database (where the data is organized as a dict like so):

{
'Package Name': {                                     
        'URL': ['http://asdfasdfasdf','http://.patch'],   
        'Dependencies': [
                'first', 'second'             
            ]
        }
}

and add the (level 1) dependencies to pkg_list, then find the dependencies of those packages (level 2) until there are no more dependencies left to list. The list is then reversed so the dependencies are installed before the desired package.


Some dependencies share dependencies and in order to prevent an infinite loop, they need to be moved to the end of the list. The problem is, when I pop a list item off in order to move it to the end, the for loop skips the next item in the list - probably something to do with the way Python indexes the list during the loop.
My question is, is it possible to pop an item off a list during iteration without Python skipping anything? P.S. Any other general tips/comments are welcome!
Maslin
  • 133
  • 5

0 Answers0