0

I have this dict:

d = { 'car'  : ["ford", "fiat", "vw"],
      'bike' : ["fant","xorg","niky"]}

And I am getting 3 string input from user:

a=str(input())
b=str(input())
c=str(input())

If the inputs are:

ford
fiat
vw

I should get "car" as output

I am trying something like this:

for a,b,c in d.values():
    print(d.keys())

but its not working, of course as it will print all d.keys no matter what the inputs are. Can anyone help me, please?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
merchmallow
  • 774
  • 3
  • 15

2 Answers2

2

Try a list comprehension:

print(next((k for k, v in d.items() if v == [a, b, c])))

Example output:

ford
fiat
vw
car

Note: inputs are always strings, so str(...) is not required, you could just use:

a = input()
b = input()
c = input()
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0
d = { 'car'  : ["ford", "fiat", "vw"],
      'bike' : ["fant","xorg","niky"]}
a=str(input())
for k, v in d.items():
    if a in v:
        print(k)
        break
moumou liu
  • 66
  • 6
  • moumou Thanks. Prob with your answer is that sometimes a we can have one of the three values with more than 1 key. I needed the the exactly 3 values to match a key as @U12-Forward did. Thank you very much for your help tho xD โ€“ merchmallow Aug 25 '21 at 02:05