houses={'apartment':15, 'penthouse':35, 'others':[20,5,70]}
what I need is to check and then find 20 for example.
been trying for hours.
would be great if you could provide and explanation and multiple solutions.
thanks in advance.
houses={'apartment':15, 'penthouse':35, 'others':[20,5,70]}
what I need is to check and then find 20 for example.
been trying for hours.
would be great if you could provide and explanation and multiple solutions.
thanks in advance.
Your houses
is a dictionary. It is missing a }
though. Its others
key is a list, so you can access its first item like so: print (houses['others'][0]). If you need to iterate over the keys and values, there are several ways as demonstrated in this link:Iterating over dict values
. A basic version is like this:
houses={'apartment':15, 'penthouse':35, 'others':[20,5,70]}
bool = False
for (k,v) in houses.items():
if type(v)==list:
if 20 in v:
bool = True
print(k,v)
if v == 20:
bool = true
print(k,v)
print(bool)
This will look over all the entries in houses
, and look for search
in the values. For each value, if it's not a collection, it will compare directly.
def is_in_house(search, house):
for v in houses.values():
try:
if search in v:
return True
except TypeError:
if v == search:
return True
return False