Reputation: 23
I am new to Python and I tried a lot of combinations, but I do not get the proper solution. I have a list in a list:
AnimalLarge = ['Animal', 'Large', 20,30]
AnimalSmall = ['Animal', 'Small', 20,10]
HumanLarge = ['Human',' Large', 1, 2]
HumanSmall = ['Human', 'Small', 2, 2]
List = [AnimalLarge, AnimalSmall, HumanLarge, HumanSmall]
Search1 = 'Animal'
Search2 = 'Small'
ResultShouldBe = AnimalSmall
If I have e.g. the variable "Animal" and "Small", then I wanted to let the program "find" the proper combination "AnimalSmall", which contains "Animal, Small, 20, 10". I know how to read out "manually" a list of a list or to read out single parameters from a list, but I am interested in reading out a list (with defined strings) within a list. I did a lot of trials with "for" and "in" and indexing, but they were all meaningless, so I did not post it here. Could you please give me a hint, how to combine this? Sorry, I really spent quite some time on it and I do not get it...
Upvotes: 1
Views: 65
Reputation: 23
Thank you so much!!!
(It looks so easy now, but i really could not manage the proper syntax, though looking on numerous documentations and forums in the web)
Upvotes: 1
Reputation: 17322
you can use a for loop:
for l in List:
if Search1 in l and Search2 in l:
result = l
break
or a list comprehension:
result = [l for l in List if Search1 in l and Search2 in l][0]
print(result)
output:
['Animal', 'Small', 20, 10]
Upvotes: 0