0

i'm writeing a program in python thats used for sorting files but i have run into the issue that i need to run though 2 lists for a command i have a list of files and a list of file destinations and i need to combine the 2 lists but i couldn't find any solutions on the internet but it just move's one file and delets the other

my code i have tried

        for x in org_path:
            for y in dest_path:
                try:
                    shutil.move(x, y)
                except:
                    print("error")```
  • 2
    ``for x, y in zip(org_path, dest_path):`` ? – Cubix48 Mar 29 '22 at 15:19
  • The code you provided is not doing what you think it does. Let's say each `x` and `y` both contain 5 objects. For `x[0]` it's going to execute `move(x[0], y[0])`, then `move(x[0], y[1])`, etc. – Teejay Bruno Mar 29 '22 at 15:23

1 Answers1

1

zip() function creates an iterator that will aggregate elements from two or more iterables. You can use the resulting iterator to quickly and consistently solve common programming problems.

a = [4,5,6]
b = [1,2,3]


for x, y in zip(a,b):
    print(x, y)

note* number of iterations is equal to the length of the shortest list

Egor Busko
  • 66
  • 5