Check if a specific Key and a value exist in a dictionary

前端 未结 3 1229
一整个雨季
一整个雨季 2021-01-16 06:42

I am trying to determine if a specific key and value pair exist in a dictionary; however, if I use the contains or has-key method, it only checks for the key. I need it to c

3条回答
  •  悲&欢浪女
    2021-01-16 07:21

    How about this function:

    def checkKeyValuePairExistence(dic, key, value):
        try:
            return dic[key] == value
        except KeyError:
            return False
    

    If you are using another type of dictionary other then the one python offers (I'm sorry, I couldnt understand from your post if you are using it or not) then let me know and i'll try to give your another solution

提交回复
热议问题