{"id":329317,"date":"2023-10-05T04:31:11","date_gmt":"2023-10-05T04:31:11","guid":{"rendered":"https:\/\/pythonguides.com\/?p=17344"},"modified":"2023-10-05T04:31:11","modified_gmt":"2023-10-05T04:31:11","slug":"python-dictionary-contains-6-example","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/10\/05\/python-dictionary-contains-6-example\/","title":{"rendered":"Python Dictionary contains (6+ Example)"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p>In this Python Tutorial, I will explain what the <strong>Python Dictionary contains<\/strong> means and how we can check what the dictionary contains in Python using different methods with examples.<\/p>\n<h2 class=\"wp-block-heading\">What does the Python Dictionary contains mean?<\/h2>\n<p>In Python, dictionaries are a flexible way to store and manipulate data. We use the term &#8220;<strong>Python Dictionary Contains<\/strong>&#8221; to describe the action of checking whether a specific key or value is present within a dictionary Python. In this article, we will go over various ways to determine if a dictionary contains a certain element, key, or value, with relevant examples.<\/p>\n<h3 class=\"wp-block-heading\">Python Dictionary Contains Key<\/h3>\n<p>To check if a Python dict contains a specific key, you can use the in keyword. This is an essential task because attempting to access a key that does not exist will result in a KeyError.<\/p>\n<p><strong>For instance<\/strong>: Consider a program that needs to find the capital of a U.S. state. We have a dictionary of states and their capitals.<\/p>\n<pre class=\"wp-block-code\"><code>states_capitals = {\n    'California': 'Sacramento',\n    'Texas': 'Austin',\n    'Florida': 'Tallahassee',\n}\n\nprint('California' in states_capitals)<\/code><\/pre>\n<p><strong>Output:<\/strong> In this example, &#8216;<strong>California&#8217; in states_capitals<\/strong> returns <strong>True<\/strong>, confirming that the dictionary contains the key &#8216;<strong>California<\/strong>&#8216;.<\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"509\" height=\"441\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-Key.jpg\" alt=\"Python dictionary contains Key example\" class=\"wp-image-45543\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-Key.jpg 509w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-Key-300x260.jpg 300w\" sizes=\"(max-width: 509px) 100vw, 509px\" title=\"Python dictionary contains Key\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Python Dictionary Contains Value<\/h3>\n<p>To verify if a Python dictionary contains the value, use the <strong>values()<\/strong> method followed by the in keyword.<\/p>\n<p><strong>For example<\/strong>: Now, imagine you want to check if a specific city is the capital of any U.S. state in the above <strong>states_capitals<\/strong> dictionary in Python.<\/p>\n<pre class=\"wp-block-code\"><code>states_capitals = {\n    'California': 'Sacramento',\n    'Texas': 'Austin',\n    'Florida': 'Tallahassee',\n}\n\nprint('Austin' in states_capitals.values())<\/code><\/pre>\n<p><strong>Output: <\/strong>Here, &#8216;<strong>Austin<\/strong>&#8216; in <strong>states_capitals.values()<\/strong> returns <strong>True<\/strong>, indicating that &#8216;<strong>Austin<\/strong>&#8216; is a capital in the <strong>states_capitals<\/strong> dictionary in Python.<\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"449\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-key-python.jpg\" alt=\"dictionary contains key python\" class=\"wp-image-45544\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-key-python.jpg 548w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-key-python-300x246.jpg 300w\" sizes=\"(max-width: 548px) 100vw, 548px\" title=\"dictionary contains key python\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Python Dictionary Contains List as Value<\/h3>\n<p>Sometimes, a <strong>dictionary contains a list of values<\/strong> where the key is associated with a list of values.<\/p>\n<p><strong>For instance<\/strong>, In a program keeping track of major cities in each state, you want to check if a particular city is in the list of major cities for a specific state.<\/p>\n<pre class=\"wp-block-code\"><code>states_cities = {\n    'California': &#91;'Los Angeles', 'San Francisco', 'San Diego'],\n    'Texas': &#91;'Houston', 'Dallas', 'San Antonio'],\n}\n\nprint('San Francisco' in states_cities&#91;'California'])<\/code><\/pre>\n<p><strong>Output:<\/strong> In this code of Python, &#8216;<strong>San Francisco<\/strong>&#8216; in <strong>states_cities[&#8216;California&#8217;]<\/strong> returns <strong>True<\/strong>, confirming that &#8216;<strong>San Francisco<\/strong>&#8216; is one of the major cities in California.<\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"412\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-key.jpg\" alt=\"python dict contains key\" class=\"wp-image-45545\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-key.jpg 761w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-key-300x162.jpg 300w\" sizes=\"(max-width: 761px) 100vw, 761px\" title=\"python dict contains key\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Python Dictionary Contains Another Dictionary<\/h3>\n<p>In some scenarios, a Python dictionary contains another dictionary as its value. They are called <strong>nested dictionary in Python<\/strong>.<\/p>\n<p><strong>For instance<\/strong>, Consider you are managing a database of U.S. states, with each state having a nested Python dictionary of additional information, and you need to check for a specific key in the nested dictionary.<\/p>\n<pre class=\"wp-block-code\"><code>states_details = {\n    'California': {\n        'Capital': 'Sacramento',\n        'Population': 39538223,\n    },\n    'Texas': {\n        'Capital': 'Austin',\n        'Population': 29145505,\n    },\n}\n\nprint('Capital' in states_details&#91;'California'])<\/code><\/pre>\n<p><strong>Output:<\/strong> In this case, &#8216;<strong>Capital<\/strong>&#8216; in <strong>states_details[&#8216;California&#8217;]<\/strong> returns <strong>True<\/strong>, indicating that the nested dictionary contains the key &#8216;<strong>Capital<\/strong>&#8216;.<\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"607\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-contains-key.jpg\" alt=\"python contains key\" class=\"wp-image-45546\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-contains-key.jpg 586w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-contains-key-290x300.jpg 290w\" sizes=\"(max-width: 586px) 100vw, 586px\" title=\"python contains key\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Python Dictionary Contains Duplicate Keys<\/h3>\n<p>A <strong>Python dictionary contains multiple keys<\/strong> but cannot have duplicate keys. Each key must be unique. If a duplicate key is found, the last occurrence will override the previous values.<\/p>\n<p><strong>For example<\/strong>, In case you accidentally define a dictionary with duplicate keys, Python will retain the last occurrence.<\/p>\n<pre class=\"wp-block-code\"><code>duplicate_keys = {\n    'a': 1,\n    'b': 2,\n    'a': 3,\n}\n\nprint(duplicate_keys)<\/code><\/pre>\n<p><strong>Output: <\/strong>Here, even though the dictionary is defined with duplicate keys, the output is <strong>{&#8216;a&#8217;: 3, &#8216;b&#8217;: 2}<\/strong>, retaining the <strong>last occurrence<\/strong> of &#8216;<strong>a<\/strong>&#8216;.<\/p>\n<pre class=\"wp-block-code\"><code>{'a': 3, 'b': 2}<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"438\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-multiple-keys.jpg\" alt=\"Python dictionary contains multiple keys\" class=\"wp-image-45547\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-multiple-keys.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-multiple-keys-205x300.jpg 205w\" sizes=\"(max-width: 300px) 100vw, 300px\" title=\"Python dictionary contains multiple keys\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Python dictionary contains key value pair<\/h3>\n<p>For checking <strong>Python dictionary contains a key-value pair<\/strong>, utilize items() alongside the in keyword.<\/p>\n<p><strong>For example<\/strong>, Let&#8217;s take a record of the participants and their selected subjects for the quiz and check for one of them with their selected subject,<\/p>\n<pre class=\"wp-block-code\"><code>Name_with_subject = {'John': &#91;'Math', 'Science'], 'Emily': &#91;'English', 'History']}\nif ('John', &#91;'Math', 'Science']) in Name_with_subject.items():\n    print(\"Yes\")<\/code><\/pre>\n<p><strong>Output:<\/strong> Here, as the name is in the dictionary so, the if condition holds <strong>True<\/strong> and gives the desired output.<\/p>\n<pre class=\"wp-block-code\"><code>Yes<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"989\" height=\"299\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-key-value-pair-1.jpg\" alt=\"Python dictionary contains key value pair\" class=\"wp-image-45549\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-key-value-pair-1.jpg 989w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-key-value-pair-1-300x91.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-dictionary-contains-key-value-pair-1-768x232.jpg 768w\" sizes=\"(max-width: 989px) 100vw, 989px\" title=\"Python dictionary contains key value pair 1\"><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">Methods to check what the Python Dictionary contains<\/h2>\n<p>There are many different methods to prevent if the Python Dictionary includes a key, values, or pair.<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Name<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>The <strong>in<\/strong> Keyword<\/td>\n<td>The <strong>in<\/strong> keyword in Python is used to <strong>check<\/strong> if a specified key exists within a dictionary.<\/td>\n<\/tr>\n<tr>\n<td>The <strong>get()<\/strong> Method<\/td>\n<td>The <strong>get()<\/strong> method in Python is used to retrieve the <strong>value for a specified key from the dictionary<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>The <strong>keys()<\/strong> Method<\/td>\n<td>The <strong>keys()<\/strong> method returns a view object that displays a <strong>list of all the keys in the dictionary.<\/strong><\/td>\n<\/tr>\n<tr>\n<td>The <strong>values() <\/strong>Method<\/td>\n<td>The <strong>values()<\/strong> method is used to <strong>check<\/strong> if a specific value exists in the dictionary.<\/td>\n<\/tr>\n<tr>\n<td>The <strong>items()<\/strong> Method<\/td>\n<td>The <strong>items()<\/strong> method returns a view object that displays a <strong>list of dictionary&#8217;s (key, value) tuple pairs<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>The <strong>all()<\/strong> Function<\/td>\n<td>The <strong>all()<\/strong> function returns <strong>True<\/strong> if all items in an iterable are <strong>true<\/strong>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table><figcaption class=\"wp-element-caption\">List of methods present in Python to check whether the dictionary has the specified key, value, or pair.<\/figcaption><\/figure>\n<p>Let&#8217;s see them one by one using demonstrative examples:<\/p>\n<h3 class=\"wp-block-heading\">Method 1: Using the in Keyword to Check for a Key<\/h3>\n<p>The in keyword in Python is used to check if a specified key exists within a dictionary. This method returns True if the key is found in the dictionary, and False otherwise. It is a fast and straightforward way to check for the existence of a key.<\/p>\n<p><strong>Scenario:<\/strong> Checking if a state is in a dictionary of U.S. states and their populations.<\/p>\n<pre class=\"wp-block-code\"><code>state_populations = {'California': 39538223, 'Texas': 29145505, 'Florida': 21538187}\n# Checking if 'Texas' is a key in the state_populations dictionary.\nif 'Texas' in state_populations:\n    print(f\"Texas has a population of {state_populations&#91;'Texas']}.\")<\/code><\/pre>\n<p><strong>Output:<\/strong> In this case, &#8216;<strong>Texas<\/strong>&#8216; is found in the<strong> state_populations<\/strong> Python dictionary, and the population is printed out.<\/p>\n<pre class=\"wp-block-code\"><code>Texas has a population of 29145505.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"348\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Dictionary-contains.jpg\" alt=\"Python Dictionary contains\" class=\"wp-image-45536\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Dictionary-contains.jpg 984w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Dictionary-contains-300x106.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Dictionary-contains-768x272.jpg 768w\" sizes=\"(max-width: 984px) 100vw, 984px\" title=\"Python Dictionary contains\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Method 2: Using the get() Method to Check for a Key:<\/h3>\n<p>The <strong>get()<\/strong> method in Python is used to retrieve the value for a specified key from the dictionary. It returns <strong>None<\/strong> if the key does not exist (unless a default value is specified). By checking the returned value, we can determine whether a key exists in the dictionary in Python.<\/p>\n<p><strong>Scenario:<\/strong> In a dictionary of national parks and their respective states, you want to find out in which state &#8220;<strong>Yosemite National Park<\/strong>&#8221; is located.<\/p>\n<pre class=\"wp-block-code\"><code>national_parks = {'Yosemite': 'California', 'Grand Canyon': 'Arizona', 'Everglades': 'Florida'}\npark = national_parks.get('Yosemite')\nif park:\n    print(f\"Yosemite National Park is in {park}.\")\nelse:\n    print(\"This national park isn't in the dictionary.\")\n<\/code><\/pre>\n<p><strong>Output:<\/strong> Here, the <strong>get() <\/strong>method returns California as &#8216;<strong>Yosemite<\/strong>&#8216; was in the <strong>national_park<\/strong> Python dictionary.<\/p>\n<pre class=\"wp-block-code\"><code>Yosemite National Park is in California.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"369\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-1024x369.jpg\" alt=\"python dict contains\" class=\"wp-image-45537\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-1024x369.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-300x108.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains-768x277.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dict-contains.jpg 1105w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"python dict contains\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Method 3: Using the keys() Method to Check for a Key<\/h3>\n<p>The <strong>keys()<\/strong> method returns a view object that displays a list of all the keys in the dictionary. You can use the in keyword to check if a specific key exists within the keys.<\/p>\n<p><strong>Scenario:<\/strong> You&#8217;re organizing a road trip and have a list of states you&#8217;ll visit. You want to confirm if &#8220;<strong>Nevada<\/strong>&#8221; is one of your destinations.<\/p>\n<pre class=\"wp-block-code\"><code>road_trip_states = {'California': 'Los Angeles', 'Arizona': 'Phoenix', 'Utah': 'Salt Lake City'}\nif 'Nevada' in road_trip_states.keys():\n    print(\"Nevada is one of the destinations!\")\nelse:\n    print(\"Nevada isn't on the list.\")\n<\/code><\/pre>\n<p><strong>Output:<\/strong> Here, the <strong>key()<\/strong> method returns <strong>False<\/strong> as &#8216;<strong>Nevada<\/strong>&#8216; is not in the <strong>road_trip_states<\/strong> dictionary in Python.<\/p>\n<pre class=\"wp-block-code\"><code>Nevada isn't on the list.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"343\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-python-contains-1024x343.jpg\" alt=\"dictionary python contains\" class=\"wp-image-45538\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-python-contains-1024x343.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-python-contains-300x100.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-python-contains-768x257.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-python-contains.jpg 1114w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"dictionary python contains\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Method 4: Using the values() Method to Check for a Value<\/h3>\n<p>The <strong>values()<\/strong> method is used to check if a specific value exists in the dictionary. It returns a view object that displays a list of all the values in the dictionary Python. You can use the in keyword to check if a particular value is present among the values.<\/p>\n<p><strong>Scenario:<\/strong> Within a Python dictionary of U.S. states and their famous foods, you want to see if any state is famous for &#8220;<strong>Burgers<\/strong>&#8220;.<\/p>\n<pre class=\"wp-block-code\"><code>state_foods = {'New York': 'Pizza', 'Maine': 'Lobster', 'Texas': 'BBQ'}\nif 'Burgers' in state_foods.values():\n    print(\"One of the states is famous for Burgers!\")\nelse:\n    print(\"No state in the list is particularly famous for Burgers.\")\n<\/code><\/pre>\n<p><strong>Output<\/strong>: In this example, the <strong>value()<\/strong> method returns <strong>False<\/strong> as &#8216;<strong>Burgers<\/strong>&#8216; is not in the <strong>state_foods<\/strong> dictionary in Python.<\/p>\n<pre class=\"wp-block-code\"><code>No state in the list is particularly famous for Burgers.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"377\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-python.jpg\" alt=\"dictionary contains python\" class=\"wp-image-45539\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-python.jpg 844w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-python-300x134.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/dictionary-contains-python-768x343.jpg 768w\" sizes=\"(max-width: 844px) 100vw, 844px\" title=\"dictionary contains python\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Method 5: Using the items() Method to Check for a Key-Value Pair<\/h3>\n<p>The <strong>items()<\/strong> method returns a view object that displays a list of dictionary&#8217;s (key, value) tuple pairs. You can use the in keyword to check if a particular key-value pair is present in the dictionary.<\/p>\n<p><strong>Scenario:<\/strong> You have a dictionary of U.S. cities and their nicknames in Python. You want to confirm that Chicago is indeed called &#8220;<strong>The Windy City<\/strong>&#8220;.<\/p>\n<pre class=\"wp-block-code\"><code>city_nicknames = {\n    'New York': 'The Big Apple',\n    'Los Angeles': 'City of Angels',\n    'Chicago': 'The Windy City'\n}\nif ('Chicago', 'The Windy City') in city_nicknames.items():\n    print(\"Yes, Chicago is called 'The Windy City'.\")\nelse:\n    print(\"This nickname doesn't match the city.\")<\/code><\/pre>\n<p><strong>Output:<\/strong> In this example, the conditional statements return <strong>True<\/strong> as <strong>(&#8216;Chicago&#8217;, &#8216;The Windy City&#8217;)<\/strong> is in the <strong>city_nicknames<\/strong> in dictionary Python.<\/p>\n<pre class=\"wp-block-code\"><code>Yes, Chicago is called 'The Windy City'.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"504\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-dictionary-python.jpg\" alt=\"contains dictionary python\" class=\"wp-image-45540\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-dictionary-python.jpg 726w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-dictionary-python-300x208.jpg 300w\" sizes=\"(max-width: 726px) 100vw, 726px\" title=\"contains dictionary python\"><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">Method 6: Using the all() Function to Check for Multiple Keys or Values<\/h3>\n<p>The <strong>all()<\/strong> function returns <strong>True<\/strong> if all items in an iterable are true. It can be used to check if multiple keys or values exist in a Python dictionary by iterating over each item and checking their existence.<\/p>\n<p><strong>Scenario for Multiple Keys:<\/strong> You&#8217;re studying famous U.S. landmarks and have a list in your dictionary. You want to ensure that both &#8220;<strong>Statue of Liberty<\/strong>&#8221; and &#8220;<strong>Mount Rushmore<\/strong>&#8221; are on your list.<\/p>\n<pre class=\"wp-block-code\"><code>landmarks = {\n    'Statue of Liberty': 'New York',\n    'Golden Gate Bridge': 'California',\n    'Mount Rushmore': 'South Dakota'\n}\nlandmarks_to_check = &#91;'Statue of Liberty', 'Mount Rushmore']\nif all(landmark in landmarks for landmark in landmarks_to_check):\n    print(f\"All landmarks, {', '.join(landmarks_to_check)}, are in the list.\")<\/code><\/pre>\n<p><strong>Output:<\/strong> Here, the <strong>all()<\/strong> function returns <strong>True<\/strong> as all keys present in the list.<\/p>\n<pre class=\"wp-block-code\"><code>All landmarks, Statue of Liberty, Mount Rushmore, are in the list.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"465\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-in-dictionary-python.jpg\" alt=\"contains in dictionary python\" class=\"wp-image-45541\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-in-dictionary-python.jpg 923w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-in-dictionary-python-300x151.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/contains-in-dictionary-python-768x387.jpg 768w\" sizes=\"(max-width: 923px) 100vw, 923px\" title=\"contains in dictionary python\"><\/figure>\n<\/div>\n<p><strong>Scenario for Multiple Values:<\/strong> In a dictionary of car manufacturers and their country of origin, you want to verify if the USA and Japan are both represented.<\/p>\n<pre class=\"wp-block-code\"><code>car_manufacturers = {'Ford': 'USA', 'Toyota': 'Japan', 'BMW': 'Germany'}\ncountries_to_check = &#91;'USA', 'Japan']\nif all(country in car_manufacturers.values() for country in countries_to_check):\n    print(f\"Both countries, {', '.join(countries_to_check)}, are present.\")<\/code><\/pre>\n<p><strong>Output:<\/strong> Here, the <strong>all()<\/strong> function returns <strong>True<\/strong> as all values present in the list.<\/p>\n<pre class=\"wp-block-code\"><code>Both countries, USA, Japan, are present.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"337\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dictionary-contain.jpg\" alt=\"python dictionary contain\" class=\"wp-image-45542\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dictionary-contain.jpg 943w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dictionary-contain-300x107.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/python-dictionary-contain-768x274.jpg 768w\" sizes=\"(max-width: 943px) 100vw, 943px\" title=\"python dictionary contain\"><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p>In summary, understanding what the <strong>Python Dictionary contains<\/strong> means, like various elements such as <strong>keys, values, pairs of both, <\/strong>or <strong>other dictionaries<\/strong>, and how we can check what the <strong>dictionary contains in Python<\/strong> using different six methods like the &#8216;<strong>in<\/strong>&#8216; keyword, <strong>get() <\/strong>method, <strong>keys()<\/strong> method, <strong>values()<\/strong> method, <strong>items()<\/strong> method or <strong>all()<\/strong> method with illustrative examples.<\/p>\n<p>Recognizing the scenarios and applying the appropriate syntax to verify the existence of elements in a Python dictionary effectively can lead to error-free coding.<\/p>\n<p>You may also like to read:<\/p>\n<ul>\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/pythonguides.com\/python-loop-through-a-list\/\"  rel=\"noreferrer noopener\" class=\"rank-math-link\">Python loop through a list<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/python-copy-file\/\"  rel=\"noreferrer noopener\">Python copy file<\/a><\/li>\n<li><a href=\"https:\/\/python-poetry.org\/docs\/\" data-type=\"link\" data-id=\"https:\/\/www.geeksforgeeks.org\/python-program-to-add-two-numbers\/?ref=lbp\"  rel=\"noreferrer noopener\">Python dependency<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/python-file-methods\/\"  rel=\"noreferrer noopener\">Python File methods<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/python-write-a-list-to-csv\/\"  rel=\"noreferrer noopener\">Python write a list to CSV<\/a><\/li>\n<li><a href=\"https:\/\/pythonguides.com\/python-dictionary-popitem-method\/\"  rel=\"noreferrer noopener\">Python dictionary popitem() method<\/a><\/li>\n<\/ul>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this Python Tutorial, I will explain what the Python Dictionary contains means and how we can check what the dictionary contains in Python using different methods with examples. What does the Python Dictionary contains mean? In Python, dictionaries are a flexible way to store and manipulate data. We use the term \u201cPython Dictionary Contains\u201d &#8230; <a title=\"Python Dictionary contains (6+ Example)\" class=\"read-more\" href=\"https:\/\/pythonguides.com\/python-dictionary-contains\/\" aria-label=\"More on Python Dictionary contains (6+ Example)\">Read more&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":2034,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python Dictionary contains (6+ Example) - ITTeacherITFreelance.hk","description":"In this Python Tutorial, I will explain what the Python Dictionary contains means and how we can check what the dictionary contains in Python using different me"},"footnotes":""},"categories":[10700],"tags":[10764],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329317"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2034"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329317"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329317\/revisions"}],"predecessor-version":[{"id":329318,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329317\/revisions\/329318"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}