legend
legend

Reputation: 58

How do I check if an item from a list is in another list?

I was trying to write a simple Python 3 program and cant find answers.

fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
favoritefruits = [fruits[0], fruits[2], fruits[3], fruits[7], fruits[8], fruits[13]]

for i in fruits:
    if fruits[i] in favoritefruits:
        print("I'm gonna buy some " + fruits[i] + " because they are one of my favorite fruits.")
    else:
        print("I'm not going to buy " + fruits[i] + ", I don't like them.")

Upvotes: 2

Views: 346

Answers (3)

Andre Nevares
Andre Nevares

Reputation: 741

You Have Two Lists: fruits and favoritefruits

fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
favoritefruits = [fruits[0], fruits[2], fruits[3], fruits[7], fruits[8], fruits[13]]

To iterate you do not need to use i.
Instead, just use:

for fruit in fruits:
    if fruit in favoritefruits:
        print("I'm gonna buy some " + fruit + " because they are one of my favorite fruits.")
    else:
        print("I'm not going to buy " + fruit + ", I don't like them.")

Output:

I'm gonna buy some strawberries because they are one of my favorite fruits.
I'm not going to buy apples, I don't like them.
I'm gonna buy some bananas because they are one of my favorite fruits.
I'm gonna buy some pomegranates because they are one of my favorite fruits.
I'm not going to buy blueberries, I don't like them.
I'm not going to buy dragon fruits, I don't like them.
I'm not going to buy papayas, I don't like them.
I'm gonna buy some pears because they are one of my favorite fruits.
I'm gonna buy some oranges because they are one of my favorite fruits.
I'm not going to buy mango, I don't like them.
I'm not going to buy tomatoes, I don't like them.
I'm not going to buy peaches, I don't like them.
I'm not going to buy melons, I don't like them.
I'm gonna buy some watermelons because they are one of my favorite fruits.

Upvotes: 0

mar1332244
mar1332244

Reputation: 1

for i in fruits will return the fruits themselves and not the index and so when you do fruits[i] python expects an int but you gave it a string. You can do for i in range(len(fruits)) so that fruits[i] returns the fruit but in your code you only use the index to get the fruit so a much cleaner solution would be to rewrite for i in fruits into for fruit in fruits and simply use fruit inside the loop.

Upvotes: 0

knosmos
knosmos

Reputation: 626

As @wkl has stated, for i in fruits will iterate through all fruit names, not the index. Replacing fruits[i] with i (though a more descriptive name is better) will fix the issue:

fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
favoritefruits = [fruits[0], fruits[2], fruits[3], fruits[7], fruits[8], fruits[13]]

for fruit in fruits:
    if fruit in favoritefruits:
        print("I'm gonna buy some " + fruit + " because they are one of my favorite fruits.")
    else:
        print("I'm not going to buy " + fruit + ", I don't like them.")

Upvotes: 3

Related Questions