Carloose
Carloose

Reputation: 29

How do I pick a string from a list and put it in a new list with the criteria

Basically my question is- Im not sure how to take a list of foods:

foods=['Steak','French Fries','Hamburger','Spinach Pasta','Omelette','beans','Chicken']

and returns veggies - Food objects from foods that are vegetarian (list of Food)

the problem is im not sure if Im supposed to make a new list and add the vegetarian food from the "foods" list to the new one or if the word "objects" has anything to do with this

also how would I be able to go through the list to check to see if it is a veg or not a veg?

this is an assignment that I have to do so I dont want any code answers please but if someone could explain so I could learn for future references that would be amazing

hopefully my question makes sense and that I am allowed to ask this type of question, thanks in advance :)

Upvotes: 0

Views: 98

Answers (1)

Ertuğrul
Ertuğrul

Reputation: 71

You need to use dict

You can create a dictionary like this:

foods = { "Steak": "not-veg", "Spinach Pasta": "veg" }

And you can check if Steak is vegetarian food like this:

if foods["Steak"] == "veg":
    print("Vegetarian food")
else:
    print("Not a vegetarian food")

This is a very naive approach. You can store boolean values in dictionary or define a check function which accepts the food name and return or print the result or you can even build a class which have a is_vegetarian method.

Upvotes: 1

Related Questions