WAMPS
WAMPS

Reputation: 349

Make sure that there are two values in a Dictionaries Key Python

I have a simple dictionary. I am able to check if one value is true. My problem arises when I need to check if two are correct. I want it to return false if one of the two are correct. But it returs True in this case

mydict = {}
mydict['Car'] = ['Diesel','Hatchback','2,ltr']
mydict['Bri'] = ['Hatchback','2ltr']


print(mydict.get('Car'))

if 'Diesel'  in mydict.get('Car'):
    print('Found')
else:
    print('This is false')

if 'Diesel' and 'Hatchback' in mydict.get('Bri'):# Here it needs these two values to be true.
    print('Found')
else:
    print('This is false')

Upvotes: 0

Views: 54

Answers (1)

JBernardo
JBernardo

Reputation: 33407

This is not being evaluated the way you think it is:

'Diesel' and 'Hatchback' in mydict.get('Bri')

But like this

'Diesel' and ('Hatchback' in mydict.get('Bri'))

So 'Diesel' evaluates to True and the second part too.

What you want is something like this:

data = mydict.get('Bri')
if 'Diesel' in data and 'Hatchback' in data:
    ...

PS: Although this question might be a duplicate of this one as marked above, the answers there are way more complex than needed for this simple case

Upvotes: 1

Related Questions