1

How to print the key of the dictionary with the value matches to the value provided

dict1 = {'A': ['1', '2'],'B': ['3', '4']}
val = ['1', '2']
for k, v in dict1.items():
    if v == val:
        print(k)

expected output : 'A'

1 Answers1

1

The error in your code is that you did not add the ending ' for the string when you set the key 'B', and running through the rest of your code, it seems fine. For reference:

dict1 = {'A': ['1', '2'],'B': ['3', '4']}
val = ['1', '2']
for k, v in dict1.items():
    if v == val:
        print(k)
AS11
  • 1,311
  • 1
  • 7
  • 17