1

I have 2 lists and want to make a for loop depending on the number of items in that list, my code:

accounts = []
passwords = []

entry1= input('Accounts : ').split()
entry2= input('Passwords : ').split()

accounts.extend(entry1)
passwords.extend(entry2)

What I want to do:

for account in accounts and password in passwords:
    # do something that matchs accounts[n] with passwords[n] each loop  
khelwood
  • 55,782
  • 14
  • 81
  • 108
T.Galisnky
  • 79
  • 1
  • 11
  • 1
    You want to iterate them in parallel, or as a cartesian product? – khelwood Jan 22 '18 at 15:45
  • 1
    Possible duplicate of [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – quamrana Jan 22 '18 at 15:48

2 Answers2

2

Take a look at zip:

https://docs.python.org/3.4/library/functions.html#zip

You would use it as

for (account, password) in zip(accounts, passwords):
    do_stuff_with_account_and_password(account, password)
lxop
  • 7,596
  • 3
  • 27
  • 42
2

It sounds like you want the zip() function:

accounts = input('Accounts : ').split()
passwords = input('Passwords : ').split()

for account, password in zip(accounts, passwords):
    #do stuff with account and password
quamrana
  • 37,849
  • 12
  • 53
  • 71