-1

Let's say I have 2 lists and a variable:

Name = "Siler City"
List1 = ["Frank", "Beth", "Jose" "Pieter"]
List2 = ["Red", "Green", "Blue", "Purple"]

This is a simple illustration of a complex problem and there is a reason I don't want to create a dictionary. These must be 2 separate list. What I want is to iterate through List1[0] and List2[0], etc... at the same time. So my desired results would be

"The Red house is owned by Frank", "The Green house is owned by Beth", "The Blue house is owned by Jose,"

etc...Why won't the below work and what is a better strategy?

for item in List1:
    if Name == "Siler City":
        for color in List2:
            print("The {} house is owned by {}".format(color, item))
gwydion93
  • 1,681
  • 3
  • 28
  • 59
  • 1
    You iterate through List2 for each item in List1 (because the second for loop is inside the body of the first for loop). – folkol Sep 04 '18 at 19:25

2 Answers2

2

You can zip your lists together to iterate over both

for list1_elm, list2_elm in zip(List1, List2):
  pass # Remove pass when you add your print statement
  # Do things Here
Nick Dima
  • 348
  • 1
  • 10
2

Use zip:

for item, color in zip(List1, List2):
    print("The {} house is owned by {}".format(color, item))
Will Vousden
  • 32,488
  • 9
  • 84
  • 95