{"id":329327,"date":"2023-10-03T08:37:15","date_gmt":"2023-10-03T08:37:15","guid":{"rendered":"https:\/\/pythonguides.com\/?p=17876"},"modified":"2023-10-03T08:37:15","modified_gmt":"2023-10-03T08:37:15","slug":"python-dictionary-multiple-keys","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/10\/03\/python-dictionary-multiple-keys\/","title":{"rendered":"Python dictionary multiple keys"},"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, we will discuss what the <strong>Python dictionary multiple keys<\/strong> are and how to <strong>create a dictionary with multiple keys in Python<\/strong>. We will see how to <strong>get multiple keys in Dictionary Python<\/strong> and how to <strong>add multi-key Dictionary Python<\/strong> using different methods present in Python. We will also see the following subtopics and will explain with some illustrative examples:<\/p>\n<ul>\n<li>Python dictionary multiple keys same value<\/li>\n<li>Python dictionary multiple keys with same name<\/li>\n<li>Python dictionary multiple keys at once<\/li>\n<li>Python dictionary multiple keys get<\/li>\n<li>Python dictionary multiple key values<\/li>\n<li>Python dictionary multiple key levels<\/li>\n<li>Python dictionary two keys<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"0-what-is-python-dictionary-multiple-keys\">What is Python dictionary multiple keys<\/h2>\n<p>In Python, a dictionary is a collection of key-value pairs where each key must be unique. We can have a <a href=\"https:\/\/pythonguides.com\/create-a-dictionary-in-python\/\" data-type=\"link\" data-id=\"https:\/\/pythonguides.com\/create-a-dictionary-in-python\/\">Python dictionary<\/a> with multiple keys in it.<\/p>\n<p><strong>Example 1: <\/strong>Let&#8217;s create a dictionary in Python with multiple keys using curly braces{}. Take some states and their capitals as the key-value pair for the dictionary.<\/p>\n<pre class=\"wp-block-code\"><code>state_capitals = {\r\n    'California': 'Sacramento',\r\n    'Texas': 'Austin',\r\n    'New York': 'Albany',\r\n    'Florida': 'Tallahassee',\r\n    'Illinois': 'Springfield'\r\n}\r\nprint(state_capitals)\r\nprint(type(state_capitals))<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"389\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-multiple-keys-1024x389.jpg\" alt=\"python dictionary with multiple keys\" class=\"wp-image-45588\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-multiple-keys-1024x389.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-multiple-keys-300x114.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-multiple-keys-768x292.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-multiple-keys.jpg 1411w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"python dictionary with multiple keys\"><\/figure>\n<\/div>\n<p>This is how we can create a Python dictionary with multiple keys using curly braces{}.<\/p>\n<p><strong>Note:<\/strong> We can also use <strong>dict() constructor<\/strong> or <strong>dictionary comprehension<\/strong> method in Python to create a dictionary with multiple keys.<\/p>\n<p><strong>Example 2:<\/strong> By using the <strong>for loop<\/strong> method we can check every value contained in the dictionary or not. To create a <strong>for loop<\/strong> use items to iterate through a dictionary of key-value pairs.<\/p>\n<ul>\n<li>In this example first, we will <strong>initialize<\/strong> a dictionary in Python with multiple keys then we create an empty Python dictionary in which the result has been stored in the form of a key-value pair. When looping through that Python dictionary, the return value is the key of that Python dictionary.<\/li>\n<\/ul>\n<p><strong>Source Code:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>new_dict = {}\r\nnew_k = &#91;'micheal', 'George', 'Oliva']\r\nfor org_key in new_k:\r\n\tnew_dict&#91;org_key] = 6\r\nprint(\"Create dictinary with multiple keys:\\n\",new_dict)<\/code><\/pre>\n<p>Here is the execution of the following given code:<\/p>\n<pre class=\"wp-block-code\"><code>Create dictinary with multiple keys:\r\n {'micheal': 6, 'George': 6, 'Oliva': 6}<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"411\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-two-keys-python.jpg\" alt=\"dictionary with two keys python\" class=\"wp-image-45589\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-two-keys-python.jpg 683w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-two-keys-python-300x181.jpg 300w\" sizes=\"(max-width: 683px) 100vw, 683px\" title=\"dictionary with two keys python\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"1-how-to-add-multiple-keys-in-python-dictionary\">How to add multiple keys in Python dictionary<\/h3>\n<p>In Python, dictionaries are <strong>mutable<\/strong> collections of items(key-value pair), i.e., We can manipulate data stored inside the Python dictionary using different methods. Let&#8217;s see some examples:<\/p>\n<p>To Create a dictionary with multiple keys and then add multiple key-value pairs to it at a time we can use the <strong>update() method<\/strong> or <strong>dictionary unpacking(*)<\/strong> in Python:<\/p>\n<p><strong>Example 2:<\/strong> Let&#8217;s take a scenario, where we are building a system to store state capitals for a USA geography quiz application. We initially starts with a few states and their capitals. As we gather more data, we want to add more states and their capitals to our dictionary in Python.<\/p>\n<pre class=\"wp-block-code\"><code>state_capitals = {\r\n    'California': 'Sacramento',\r\n    'Texas': 'Austin'\r\n}\r\nnew_capitals = {\r\n    'Florida': 'Tallahassee',\r\n    'Georgia': 'Atlanta'\r\n}\r\nstate_capitals.update(new_capitals)\r\nprint(state_capitals)<\/code><\/pre>\n<p><strong>Output:<\/strong> With the <strong>update()<\/strong> method, we&#8217;ve successfully added multiple <strong>new states (keys)<\/strong> and their <strong>capitals (values)<\/strong> to the original <strong>state_capitals<\/strong> dictionary in Python.<\/p>\n<pre class=\"wp-block-code\"><code>{'California': 'Sacramento', 'Texas': 'Austin', 'Florida': 'Tallahassee', 'Georgia': 'Atlanta'}<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"496\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-two-keys-1024x496.jpg\" alt=\"python dictionary with two keys\" class=\"wp-image-45590\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-two-keys-1024x496.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-two-keys-300x145.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-two-keys-768x372.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-two-keys.jpg 1112w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"python dictionary with two keys\"><\/figure>\n<\/div>\n<p><strong>Note :<\/strong> We can also <strong>unpack using (*)<\/strong> and <strong>concatenate using (+)<\/strong>, these two dictionaries. <\/p>\n<p><strong>Example 2:<\/strong> To Create a <strong>Python dictionary<\/strong> with multiple keys we can apply the mapped method with a single value and it allows a user to update the value at all keys at the same time.<\/p>\n<pre class=\"wp-block-code\"><code>new_lis = &#91;1,2,3]\n\nyou_dict = {\"John\" : new_lis, \"Potter\" : new_lis}\nprint(you_dict)<\/code><\/pre>\n<p>In the above code first, we will initialize a Python list and then use map string values to an object. As you can see the output will display that every key contains its own value which is [1,2,3] and hence, we have created a dictionary through Python.<\/p>\n<p>Here is the execution of the following  given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"350\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-one-value.jpg\" alt=\"python dictionary two keys one value\" class=\"wp-image-45591\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-one-value.jpg 631w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-one-value-300x166.jpg 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" title=\"python dictionary two keys one value\"><\/figure>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"350\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-one-value.jpg\" alt=\"python dictionary two keys one value\" class=\"wp-image-45591\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-one-value.jpg 631w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-one-value-300x166.jpg 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" title=\"python dictionary two keys one value\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys methods<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"2-by-using-tuples-with-multiple-keys\">By using tuples with multiple keys of dictionary in Python<\/h3>\n<ul>\n<li>Here we can see how to use the<strong> Python tuple<\/strong> concept while creating a dictionary with multiple keys in Python.<\/li>\n<li>To do this particular task first we will initialize an empty Python dictionary and then use values in the form of tuples. In Python dictionary, tuples are hashable objects and can be used as a key or value. After that check the condition if &#8216;key&#8217; contains in the given list or not.<\/li>\n<\/ul>\n<p><strong>Source Code:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>my_dict={}\nmy_dict&#91;('Country','Name')]='Germany'\nmy_dict&#91;('Ipl','year')]='2014'\nmy_dict&#91;('student','name')]='Elite'\nprint(my_dict)\n\nnew_key=('Country','Name')\nif new_key in my_dict:\n    print( my_dict&#91;new_key] )<\/code><\/pre>\n<p>Here is the Screenshot of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-2-keys-1024x526.jpg\" alt=\"python dictionary with 2 keys\" class=\"wp-image-45592\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-2-keys-1024x526.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-2-keys-300x154.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-2-keys-768x394.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-with-2-keys.jpg 1050w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"python dictionary with 2 keys\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys <\/figcaption><\/figure>\n<\/div>\n<p>Read: <a rel=\"noreferrer noopener\" href=\"https:\/\/pythonguides.com\/python-find-max-value-in-a-dictionary\/\" >Python find max value in a dictionary<\/a><\/p>\n<h2 class=\"wp-block-heading\" id=\"3-python-dictionary-multiple-keys-same-value\">Python dictionary multiple keys same value<\/h2>\n<ul>\n<li>Let us see how to assign the same value to multiple keys in a Python dictionary.<\/li>\n<li>Here we can use the concept of <strong>dict.keys()<\/strong> method that will return an object that displays a dictionary in Python of all the keys which is present in the <strong>Python tuple<\/strong>.<\/li>\n<\/ul>\n<p><strong>Source Code:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>class new_dict(dict):\n    def keylist(self, new_k, new_val):\n        for n_key in new_k:\n            self&#91;n_key] = new_val\n\nb = new_dict()\nb.keylist(('l', 'o', 'i'), 5)\nprint(b)<\/code><\/pre>\n<p>In the above code first, we will create a function &#8216;<strong>new_dict<\/strong>&#8216; and assign a Python dictionary &#8216;<strong>dict<\/strong>&#8216;. Now use <strong>for loop<\/strong> method to iterate &#8216;<strong>keylist<\/strong>&#8216; and display multiple keys with the same value.<\/p>\n<p>Here is the execution of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"477\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dict-with-multiple-keys.jpg\" alt=\"python dict with multiple keys\" class=\"wp-image-45593\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dict-with-multiple-keys.jpg 537w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dict-with-multiple-keys-300x266.jpg 300w\" sizes=\"(max-width: 537px) 100vw, 537px\" title=\"python dict with multiple keys\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys same value<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"4-check-multiple-keys-in-dictionary-with-same-value\">Check multiple keys in dictionary with same value in Python<\/h3>\n<p>In this example, we can apply the concept of the <strong>dict comprehension<\/strong> method. In Python, the <strong>dictionary comprehension<\/strong> method is to create a dictionary by merging two sets of data which are in the form of Python lists.<\/p>\n<pre class=\"wp-block-code\"><code>you_dictionary = {\n    new_key: new_val\n    for keys, new_val in &#91;(&#91;'y', 'q', 'x'], 40), (&#91;'i', 'l'], 40)]\n    for new_key in keys\n    }\nprint(\"Multiple keys same value:\",you_dictionary)<\/code><\/pre>\n<p>In the above code first, we will initialize a Python dictionary and assign them a <strong>dictionary comprehension <\/strong>method as an argument. Now I have to store multiple Keys with the associated value and contains them in a Python list.<\/p>\n<p>Here is the output of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"827\" height=\"403\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-multi-key-dictionary.jpg\" alt=\"python multi key dictionary\" class=\"wp-image-45594\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-multi-key-dictionary.jpg 827w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-multi-key-dictionary-300x146.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-multi-key-dictionary-768x374.jpg 768w\" sizes=\"(max-width: 827px) 100vw, 827px\" title=\"python multi key dictionary\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys same value<\/figcaption><\/figure>\n<\/div>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/how-to-create-an-empty-python-dictionary\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to create an empty Python dictionary<\/a><\/p>\n<h2 class=\"wp-block-heading\" id=\"5-python-dictionary-multiple-keys-with-same-name\">Python dictionary multiple keys with same name<\/h2>\n<ul>\n<li>In Python dictionary, if you want to display multiple keys with the same value then you have to use the concept of <strong>for loop<\/strong> and <strong>list comprehension<\/strong> in Python methods.<\/li>\n<li>Here we create a Python list and assign them a value<strong> 2<\/strong> which means the keys will display the same name two times. After that declare a variable &#8216;<strong>new_val<\/strong>&#8216; and assign them values in the form of a list in Python.<\/li>\n<li>Now initialize an empty Python dictionary and use the concept of <strong>for loop<\/strong> to iterate over the Python list and apply the <strong>list comprehension<\/strong> to allow you to create lists from existing lists.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>new_lis = &#91;2]\nnew_val = &#91;&#91;18, 21, 56], &#91;14, 10, 61]]\n\nmy_new_dictionary = {}\n\nfor m in new_lis:\n    my_new_dictionary&#91;m] = new_val\n\nfor key,val in my_new_dictionary.items():\n    for val in my_new_dictionary&#91;key]:\n        print(\"new_key: {} :: same_nam_val: {}\".format(key, val))<\/code><\/pre>\n<p>Here is the implementation of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"793\" height=\"574\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-2-keys.jpg\" alt=\"python dictionary 2 keys\" class=\"wp-image-45595\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-2-keys.jpg 793w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-2-keys-300x217.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-2-keys-768x556.jpg 768w\" sizes=\"(max-width: 793px) 100vw, 793px\" title=\"python dictionary 2 keys\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys with the same name<\/figcaption><\/figure>\n<\/div>\n<p>As you can see from the Output, the multiple keys have the same name in a Python dictionary.<\/p>\n<h2 class=\"wp-block-heading\" id=\"6-python-dictionary-multiple-keys-get\">Python dictionary multiple keys get<\/h2>\n<ul>\n<li>Here we can see how to get multiple keys in a Python dictionary.<\/li>\n<li>To do this task we can use <strong>for loop<\/strong> method to check that every value contains in the Python dictionary or not. To declare a <strong>for loop<\/strong> method use items to iterate through a Python dictionary key-value pairs.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>my_dict = {}\n\nnew_k = &#91;'lichard', 'John', 'james']\nfor org_key in new_k:\n\tmy_dict&#91;org_key] = 8\n\nprint(\"Get multiple keys with value:\",my_dict)<\/code><\/pre>\n<p>In the above code first, we will create an empty Python dictionary and along with that declare a variable assign them a key list with the associated value that is <strong>8<\/strong>. Now display the result and you can get multiple keys with values from a Dictionary in Python.<\/p>\n<p>Here is the Output of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"821\" height=\"442\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/Python-dictionary-multiple-keys.jpg\" alt=\"Python dictionary multiple keys\" class=\"wp-image-45596\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/Python-dictionary-multiple-keys.jpg 821w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/Python-dictionary-multiple-keys-300x162.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/Python-dictionary-multiple-keys-768x413.jpg 768w\" sizes=\"(max-width: 821px) 100vw, 821px\" title=\"Python dictionary multiple keys\"><figcaption class=\"wp-element-caption\">Python dictionary multiple keys get<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"7-python-dictionary-multiple-key-values\">Python dictionary multiple key values<\/h2>\n<ul>\n<li>Let us see how to get multiple keys-values from Python dictionary.<\/li>\n<li>In this example, if you want to get multiple keys-values then you need to associate an object with each key as a value. In the given <strong>Python dict<\/strong> the keys are strings and with every single key, we assign a list of numbers as a value.<\/li>\n<\/ul>\n<p><strong>Source Code:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>my_dict = {'n': &#91;4,2,3,4],\n             'z': &#91;18,23,45,7,],\n             'u': &#91;78,34,5,6],\n             'q': &#91;19,10,12,34],\n             'p': &#91;446,76,5,9]}\n\nnew_val = my_dict&#91;'q']\nprint('Multiple Values of key \"q\" are:',new_val)<\/code><\/pre>\n<p>In the above Code, we choose an item from the Python dict where the key is &#8216;<strong>q<\/strong>&#8216; and it will return all the values associate with the key.<\/p>\n<p>Here is the execution of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"465\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-multiple-keys.jpg\" alt=\"Python dictionary with multiple keys\" class=\"wp-image-45597\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-multiple-keys.jpg 646w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-multiple-keys-300x216.jpg 300w\" sizes=\"(max-width: 646px) 100vw, 646px\" title=\"dictionary with multiple keys\"><figcaption class=\"wp-element-caption\">Python dictionary multiple key values<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"8-python-dictionary-multiple-key-levels\">Python dictionary multiple key levels<\/h2>\n<ul>\n<li>Here we can see how to get multiple key levels from the dictionary in Python.<\/li>\n<li>To solve this problem we can use the concept of the <strong>defaultdict()<\/strong> method to get multiple key levels. In Python, the <strong>defaultdict()<\/strong> will simply create any items that you want to try access. The method always returns a <strong>dictionary-like object<\/strong>.<\/li>\n<li>Now if you want to open a file in the writing mode then you can use the<strong> With open()<\/strong> function in Python. For this, you also need to import a <strong>CSV <\/strong>and <strong>collection<\/strong> module.<\/li>\n<\/ul>\n<p><strong>Source Code:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>import collections\nimport csv\n\nnew_out = collections.defaultdict(int)\n\nwith open(\"test9.csv\",\"rt\") as f:\n    for line in f:\n        new_val = line.split()\n        new_key = '-'.join(new_val)\n        new_out&#91;new_key] += 1\n\nprint (new_out)<\/code><\/pre>\n<p>Here is the Output of the following given Python code:<\/p>\n<div class=\"wp-block-columns is-layout-flex wp-container-3 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"573\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-select-multiple-keys.jpg\" alt=\"python dictionary select multiple keys\" class=\"wp-image-45598\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-select-multiple-keys.jpg 770w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-select-multiple-keys-300x223.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-select-multiple-keys-768x572.jpg 768w\" sizes=\"(max-width: 770px) 100vw, 770px\" title=\"python dictionary select multiple keys\"><figcaption class=\"wp-element-caption\">Python dictionary multiple key levels<\/figcaption><\/figure>\n<\/div>\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"370\" height=\"157\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-two-keys.jpg\" alt=\"Python dictionary with two keys\" class=\"wp-image-45599\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-two-keys.jpg 370w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/dictionary-with-two-keys-300x127.jpg 300w\" sizes=\"(max-width: 370px) 100vw, 370px\" title=\"dictionary with two keys\"><\/figure>\n<\/div>\n<\/div>\n<p>As you can see the output it displays multiple keys level in the form of a dictionary in Python.<\/p>\n<h2 class=\"wp-block-heading\" id=\"9-python-dictionary-two-keys\">Python dictionary two keys<\/h2>\n<ul>\n<li>Let us see how to combine two keys and get multiple key-value in Python dictionary.<\/li>\n<li>Here we create a Python list and assign them a value <strong>2<\/strong> which means the keys will display the same name two times. After that declare a variable &#8216;<strong>new_val<\/strong>&#8216; and assign them values in the form of a Python list.<\/li>\n<li>In this example, you can use any object as a key and contains a value in the form of a list in Python dict.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>lis1 = &#91;2]\nnew_val2 = &#91;&#91;18, 21, 56], &#91;14, 10, 61]]\n\nyou_dictionary = {}\n\nfor z in lis1:\n    you_dictionary&#91;z] = new_val2\n\nfor key,val in you_dictionary.items():\n    for val in you_dictionary&#91;key]:\n        print(\"new_key: {} :: same_nam_val: {}\".format(key, val))<\/code><\/pre>\n<p>Here is the Output of the following given code:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys.jpg\" alt=\"python dict multiple keys\" class=\"wp-image-45600\" style=\"width:786px;height:607px\" width=\"786\" height=\"607\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys.jpg 786w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-300x232.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2021\/09\/python-dictionary-two-keys-768x593.jpg 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" title=\"python dictionary two keys\"><figcaption class=\"wp-element-caption\">Python dict with two keys<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p>In this Python tutorial, we have discussed what the <strong>Python dictionary multiple keys<\/strong> are and how to <strong>create a dictionary with multiple keys in Python<\/strong>. We have also seen, how to <strong>get multiple keys in Dictionary Python<\/strong> and how to <strong>add multi-key Dictionary Python<\/strong> using different methods present in Python. We have used different methods present in Python like for loop, dictionary comprehension, update(), defaultdict(), etc. to get the desired output.<\/p>\n<p>We have taken different demonstartive examples to better understand all the above mentioned concept in Python.<\/p>\n<p>You may also like to read the following tutorials.<\/p>\n<ul>\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/pythonguides.com\/python-dictionary-pop\/\"  rel=\"noreferrer noopener\" class=\"rank-math-link\">Python dictionary pop<\/a><\/li>\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)\" href=\"https:\/\/pythonguides.com\/python-convert-dataframe-to-list\/\"  rel=\"noreferrer noopener\" class=\"rank-math-link\">Python convert DataFrame to list<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/pythonguides.com\/get-first-key-in-dictionary-python\/\"  rel=\"noreferrer noopener\" class=\"rank-math-link\">Get First Key in dictionary Python<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/pythonguides.com\/python-dictionary-increment-value\/\"  rel=\"noreferrer noopener\" class=\"rank-math-link\">Python dictionary increment value<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/python-for-loop-index\/\"  rel=\"noreferrer noopener\">Python for loop index<\/a><\/li>\n<li><a href=\"https:\/\/www.mongodb.com\/community\/forums\/t\/mongodb-allows-multiple-keys-with-the-same-name\/754\"  data-type=\"link\" data-id=\"https:\/\/www.mongodb.com\/community\/forums\/t\/mongodb-allows-multiple-keys-with-the-same-name\/754\" rel=\"noreferrer noopener\">Mongodb allows multiple keys<\/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, we will discuss what the Python dictionary multiple keys are and how to create a dictionary with multiple keys in Python. We will see how to get multiple keys in Dictionary Python and how to add multi-key Dictionary Python using different methods present in Python. We will also see the following &#8230; <a title=\"Python dictionary multiple keys\" class=\"read-more\" href=\"https:\/\/pythonguides.com\/python-dictionary-multiple-keys\/\" aria-label=\"More on Python dictionary multiple keys\">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 multiple keys - ITTeacherITFreelance.hk","description":"In this Python tutorial, we will discuss what the Python dictionary multiple keys are and how to create a dictionary with multiple keys in Python. We will see h"},"footnotes":""},"categories":[10700],"tags":[10771],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329327"}],"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=329327"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329327\/revisions"}],"predecessor-version":[{"id":329328,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329327\/revisions\/329328"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}