[Solved] Basic Python : check if property in list of lists

Hi,

I have searched the web but couldn’t find the appropriate keywords in english for the following : How can I check if a value is in a list of lists?

I know how to search if avalue is in an list :


if property in obj:
   instructions

How can I know if the value “Elisa” is in the list :
friends_List = [[“Jenny”, 25, “Australian”], [“Elisa”, 28, “French”]]

The following script doesn’t seem to work for a list with more than one “level” :


if "Elisa" in friends_List:
   instructions

I’d like to avoid doing this :

for num in range (len(friends_List)-1):
   if "Elisa" in friends_List[num]
      instructions

because that would not suit lists with too many “levels” (what’s the word for that?).

Thank you :slight_smile:

You cant avoid looking within a list of lists really, the only other thing I can suggest is use the names as a dict.


friends_Dict = {"Jenny":(25, "Australian"), "Elisa":(28, "French")}

then you can do…
if “Jenny” in friends_Dict: …

BUT you say, what if you have 2 friends called Jenny… in that case you can do…
friends_Dict = {“Jenny”:[(42, “Austria”), (25, “Australian”)], “Elisa”:[(28, “French”)]}

I’ll try to see wich solution fits my game the best.
Thank you for the quick and well-explained answer :slight_smile: