def pparent(paths): paths = sorted(paths,key=len) first = True ret = [] while len(paths) > 0: el1 = old_paths[0] old_paths = paths[1:] paths = [] for path in old_paths: if not paths.startswith(el1): paths.insert(0,path) return ret