{"id":2616,"date":"2023-10-18T13:16:40","date_gmt":"2023-10-18T07:46:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=2616"},"modified":"2023-11-10T11:46:46","modified_gmt":"2023-11-10T06:16:46","slug":"python-how-to-check-if-a-key-exists-in-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-check-if-a-key-exists-in-dictionary\/","title":{"rendered":"Python How to Check if a Key Exists in Dictionary"},"content":{"rendered":"
A Python dictionary is a list of objects that are not in any particular order i.e Unordered.<\/p>\n
A dictionary is made up of a collection of key-value pairs. Each key-value pair corresponds to a specific value.<\/p>\n
Curly braces { } can be used to describe a dictionary by enclosing a comma-separated list of key-value pairs.<\/p>\n
Every key is separated from its associated value by a colon “:”.<\/p>\n
Given a dictionary, the task is to determine whether the given key exists in the dictionary.<\/p>\n
Examples:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to check whether the given key exists in the dictionary some of them are:<\/p>\n The keys() method returns a list of all the available keys in the dictionary. Using the inbuilt method keys(), use an if statement and the \u2018in’ operator to determine whether or not the key exists in the dictionary.<\/p>\n Output:<\/strong><\/p>\n To verify if a key exists in the dictionary, we can use the \u2018in operator’ directly with the dictionary.<\/p>\n The expression:<\/p>\n If the key exists in the dictionary, it will evaluate to True; otherwise, it will evaluate to False.<\/p>\n Let’s use this to see if the key is in the dictionary.<\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n It will raise a KeyError if we try to access a value of key that does not exist in the dictionary. This can also be used to see if anything exists in the dict.<\/p>\n This can also be a way to check if exist in dictionary or not .<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The dict class in Python has a get() method that accepts a key and a default value.<\/p>\n The function’s behaviour,<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n We’ve just verified whether the key exists in the dictionary so far.<\/p>\n However, if we want to verify if a key does not exist in the dictionary, we can use \u2018if not in’ with the dictionary directly.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n A Python dictionary is a list of objects that are not in any particular order i.e Unordered. A dictionary is made up of a collection of key-value pairs. Each key-value pair corresponds to a specific value. Curly braces { } can be used to describe a dictionary by enclosing a comma-separated list of key-value pairs. …<\/p>\ndictionary = {'This': 100, 'is':200, 'python':300} , key = is<\/pre>\n
Yes<\/pre>\n
Check whether given Key already exists in a Python Dictionary<\/h2>\n
\n
Method #1: Using keys() function<\/h3>\n
# Given Dictionary\r\n\r\ndictionary = {'this': 100, 'is': 200, 'python': 300}\r\n\r\n# Given key\r\nkey = 'is'\r\n\r\n# Checking if key is exist in dictionary using keys() method\r\nif key in dictionary.keys():\r\n # If the key exits then print yes\r\n print(\"Yes\")\r\n# If the key do not exist then print no\r\nelse:\r\n print(\"No\")\r\n<\/pre>\n
Yes<\/pre>\n
Method #2: Using if-in Statements<\/h3>\n
key in dictionary<\/code><\/p>\n
# Given Dictionary\r\n\r\ndictionary = {'this': 100, 'is': 200, 'python': 300}\r\n\r\n# Given key\r\nkey = 'is'\r\n\r\n# Using if and in to check whether the key is present in dictionary\r\nif key in dictionary:\r\n # If the key exits then print yes\r\n print(\"Yes\")\r\n# If the key do not exist then print no\r\nelse:\r\n print(\"No\")\r\n<\/pre>\n
Yes<\/pre>\n
Method #3: Using List and if statement<\/h3>\n
\n
# Given Dictionary\r\ndictionary = {'this': 100, 'is': 200, 'python': 300}\r\n\r\n# Given key\r\nkey = 'is'\r\n# Converting dictionary keys to list\r\nkeyslist = list(dictionary.keys())\r\n\r\n# Checking if key is exist in list using if\r\nif key in keyslist:\r\n # If the key exits then print yes\r\n print(\"Yes\")\r\n# If the key do not exist then print no\r\nelse:\r\n print(\"No\")\r\n<\/pre>\n
Yes<\/pre>\n
Method #4: Using Exceptional Handling<\/h3>\n
# Given Dictionary\r\ndictionary = {'this': 100, 'is': 200, 'python': 300}\r\n\r\n# Given key\r\nkey = 'is'\r\n# Using try and except\r\ntry:\r\n # If the key exist in dictionary its value is assigned to test and then it prints yes\r\n test = dictionary[key]\r\n print(\"Yes\")\r\n# If the key do not exist in dictionary it prints no\r\nexcept KeyError:\r\n print(\"No\")\r\n<\/pre>\n
Yes<\/pre>\n
Method #5: Using get() function<\/h3>\n
dictionary.get(keyname, value)<\/code><\/p>\n
\n
# Given Dictionary\r\ndictionary = {'this': 100, 'is': 200, 'python': 300}\r\n\r\n# Given key\r\nkey = 'is'\r\n# Using get()\r\nif dictionary.get(key) is not None:\r\n print(\"Yes\")\r\nelse:\r\n print(\"No\")\r\n<\/pre>\n
Yes<\/pre>\n
Method #6 : Using ‘if not in’ statement<\/h3>\n
# Given Dictionary\r\ndictionary = {'this': 100, 'is': 200, 'python': 300}\r\n\r\n# Given key\r\nkey = 'is'\r\n# Using if not in\r\nif key not in dictionary:\r\n # if the key not in dictionary then we print no\r\n print(\"No\")\r\nelse:\r\n print(\"Yes\")\r\n<\/pre>\n
Yes<\/pre>\n
\n