0

I'm trying to practice some for loops. I would like to print one value from two separate lists and then join them together. I was able to get it to print what I wanted with if/elif statements but I don't know if there is a better way to do it.

Desired output:

John eats Mexican
Marissa eats Japanese
Pete eats French
Dayton eats American

Here is the code that I tried:

persons = [ "John", "Marissa", "Pete", "Dayton" ]
restaurants = [ "Japanese", "American", "Mexican", "French" ]

for person in persons:
    for restaurant in restaurants:
        if person == "John" and restaurant == "Mexican":
            print(person + " eats " + restaurant)
        elif person == "Pete" and restaurant == "French":
            print(person +" eats " + restaurant)
        elif person == "Dayton" and restaurant == "American":
            print(person +" eats " + restaurant)
        elif person == "Marissa" and restaurant == "Japanese":
            print(person +" eats " + restaurant)

I want to know what else is possible and what could make my code more efficient.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

2 Answers2

1

It really depends on what your input is. If you are free to use whatever data structure you want, I would use a mapping like this:

restaurant_per_person = {
    "John": "Mexican",
    "Marissa": "Japanese",
    "Pete": "French",
    "Dayton": "American"
}
for person, restaurant in restaurant_per_person.items():
    print(person + " eats " + restaurant)

Otherwise, if you have to use lists, and you can guarantee they are in the same order (ie, the first person will go to the first restaurant), you can use "zip" to iterate over the two lists at the same time:

persons = ["John", "Marissa", "Pete", "Dayton"]
restaurants = ["Mexican", "Japanese", "French", "American"]
for person, restaurant in zip(persons, restaurants):
    print(person + " eats " + restaurant)
0

Your question is a little, lets say, misguided. Let me explain you why:

By using the if statements within the two for loops you obviously have a direct connection between two datapoints within the lists. Meaning, only one direct entry in the persons lists belongs to one entry within the restaurants list. Because these lists are not ordered in any way, you have to check for every single possible combination and if the right combination occurs.

However, you would nearly never stumble across such a situation (where you know what data has to be combined to each other) and still have the trouble of going through a for loop to get that exact data, matching every single possible combination along the way.

A better practice (and a more useful one, I think) would be to have two sorted lists (meaning the entries at both lists at the same indices belong to each other) and print the results from both.

This way, you would not need to have a nested loop but still practice handling two lists at once.

Example for iterating through sorted lists:

persons = [ "John", "Marissa", "Pete", "Dayton" ]
restaurants = [ "Japanese", "American", "Mexican", "French" ]

for i in range(len(persons)):
        print(persons[i], 'eats', restaurants[i])

Output:

John eats Japanese
Marissa eats American
Pete eats Mexican
Dayton eats French
Sepheraton
  • 16
  • 4