{"id":18921,"date":"2022-01-08T13:45:18","date_gmt":"2022-01-08T08:15:18","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=18921"},"modified":"2022-01-08T13:45:21","modified_gmt":"2022-01-08T08:15:21","slug":"python-key-value-pair","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-key-value-pair\/","title":{"rendered":"7 Examples to Grasp Python Key Value Pair"},"content":{"rendered":"\n<p>The ease that comes with the python programming language is one of the reasons why it is a programmer&#8217;s favorite. It is now handling data, files, databases, or its vast community. There are several ways of storing single values elements in python. In this article, our focus is on storing data with some keys and values. We will see different ways to create objects as Python Key Value Pair.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#What_is_Key-Value_Pair_in_Python\" >What is Key-Value Pair in Python?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_1_Creating_Dictionary_Object\" >Example 1: Creating Dictionary Object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_2_Creating_Dynamic_Dictionary_Using_Class\" >Example 2: Creating Dynamic Dictionary Using Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_3_Update_Method_to_update_Items_in_Dictionary_Object\" >Example 3: Update() Method to update Items in Dictionary Object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_4_Update_Dictionary_using_Subscript_Notation\" >Example 4: Update Dictionary using Subscript Notation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_5_Key_Value_Pair_Using_ArgParse\" >Example 5: Key Value Pair Using ArgParse<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_6_Adding_Key_Value_Pair_to_CSV_File\" >Example 6: Adding Key Value Pair to CSV File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_6_Removing_Key_Value_Pair_from_Dictionary\" >Example 6: Removing Key Value Pair from Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Example_7_Key_Value_Pair_to_Dataframe\" >Example 7: Key Value Pair to Dataframe<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#FAQs_on_Python_Key_Value_Pair\" >FAQs on Python Key Value Pair<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#Trending_Python_Articles\" >Trending Python Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-key-value-pair-in-python\"><span class=\"ez-toc-section\" id=\"What_is_Key-Value_Pair_in_Python\"><\/span>What is Key-Value Pair in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In python, we have several data types used to store single value elements. However, to access elements from those objects, we need to use an index which is their positional identity. Similarly, the key value pair represents data in the form of keys and values. Every value present in the object corresponds to a key. These keys are used to access values from the object. The object which stores these key value pairs is known as Dictionary. There are several ways to define a dictionary or add a new key-value pair in existing dictionary objects. Let&#8217;s see each of them one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-1-creating-dictionary-object\"><span class=\"ez-toc-section\" id=\"Example_1_Creating_Dictionary_Object\"><\/span>Example 1: Creating Dictionary Object<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The first thing we need to understand is how to create a dictionary object for storing key value pairs. So, let&#8217;s see its syntax.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Syntax:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>dict_object = { 'key1':'value1','key2':'value2',....}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s see an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Creating a dictionary\n\ndict_object = {'a':10,'b':20,'c':30}\nprint(dict_object)\nprint(type(dict_object))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'a': 10, 'b': 20, 'c': 30}\n&lt;class 'dict'><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-2-creating-dynamic-dictionary-using-class\"><span class=\"ez-toc-section\" id=\"Example_2_Creating_Dynamic_Dictionary_Using_Class\"><\/span>Example 2: Creating Dynamic Dictionary Using Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>So, in the above example, we have seen how we can create a dictionary object. But, the problem with the above method is it is static. If we want runtime updation in the dictionary items, we must follow another approach. So, the following example demonstrates how we can create a class that accepts key value pairs and then feeds them into a dictionary object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Creating dictionary class \nclass CreateDictionary(dict): \n    def __init__(self): \n        self = dict() \n          \n    # Function to add key:value \n    def add(self, key, value): \n        self&#x5B;key] = value \n  \n# Creating dictionary object \ndict_obj = CreateDictionary() \n  \nkey = input(&quot;Enter key: &quot;)\nvalue = input(&quot;Enter value: &quot;)\n  \ndict_obj.add(key,value) \n  \nprint(dict_obj)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter key: a\nEnter value: 30\n{'a': '30'}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation\">Explanation<\/h4>\n\n\n\n<p>So, in the above example, we first created a CreateDictionary class inherited from dict class. After that, we created a function inside it to add key value pair there. In the end, we initialized its object and passed the key and values as the argument. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-3-update-method-to-update-items-in-dictionary-object\"><span class=\"ez-toc-section\" id=\"Example_3_Update_Method_to_update_Items_in_Dictionary_Object\"><\/span>Example 3: Update() Method to update Items in Dictionary Object<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>However, we can create and update a dictionary using the above method. Dict class has an update method used to add items in the dictionary object. We can use the technique in two ways.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><em>Either we pass another dictionary object as the argument of updaate method.<\/em><\/strong><\/li><li><strong><em>Or, We assign the value to a variable and pass it as the argument. Name of the variable is stored as the key of the item.<\/em><\/strong><\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Creating dictionary object\ndict_obj = {'a':'10', 'b':'20','c':'30'} \nprint(&quot;dict_obj before update &quot;, dict_obj) \n  \n# adding dict1 (key3, key4 and key5) to dict \ndict1 = {'d':'40', 'e':'50'} \ndict_obj.update(dict1) \n  \n# by assigning \ndict_obj.update(f ='60')\nprint(&quot;dict_obj after update&quot;,dict_obj)\n<\/pre><\/div>\n\n\n<p> <strong>Output:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict_obj before update  {'a': '10', 'b': '20', 'c': '30'}\ndict_obj after update {'a': '10', 'b': '20', 'c': '30', 'd': '40', 'e': '50', 'f': '60'}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-4-update-dictionary-using-subscript-notation\"><span class=\"ez-toc-section\" id=\"Example_4_Update_Dictionary_using_Subscript_Notation\"><\/span>Example 4: Update Dictionary using Subscript Notation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Although the above-specified method for dictionary updation is easy to use, the subscript notation method is highly used by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Programmer\" target=\"_blank\" rel=\"noreferrer noopener\">programmers<\/a> to update the dictionary. It is one of the easiest ways to update a dictionary object. Let&#8217;s see an example to understand how we can do that.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Creating dictionary object\ndict_obj = {'a':'100', 'b':'200','c':'300'} \nprint(&quot;dict_obj before update &quot;, dict_obj) \n    \n# using the subscript notation \n# Dictionary_Name&#x5B;New_Key_Name] = New_Key_Value \n  \ndict_obj&#x5B;'d'] = '400'\ndict_obj&#x5B;'e'] = '500'\ndict_obj&#x5B;'f'] = '600'\nprint(&quot;Updated Dict is: &quot;, dict_obj)\n<\/pre><\/div>\n\n\n<p> <strong>Output:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict_obj before update  {'a': '100', 'b': '200', 'c': '300'}\nUpdated Dict is:  {'a': '100', 'b': '200', 'c': '300', 'd': '400', 'e': '500', 'f': '600'}<\/code><\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-5-key-value-pair-using-argparse\"><span class=\"ez-toc-section\" id=\"Example_5_Key_Value_Pair_Using_ArgParse\"><\/span>Example 5: Key Value Pair Using ArgParse<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing argparse module\nimport argparse\n\nclass KeyValue(argparse.Action):\n\tdef __call__( self , parser, namespace,\n\t\t\t\tvalues, option_string = None):\n\t\tsetattr(namespace, self.dest, dict())\n\t\t\n\t\tfor value in values:\n\t\t\t# split it into key and value\n\t\t\tkey, value = value.split('=')\n\t\t\t# Adding it as dictionary\n\t\t\tgetattr(namespace, self.dest)&#x5B;key] = value\n\n# Initializing parser object\nparser = argparse.ArgumentParser()\n\n# adding an arguments\nparser.add_argument('--kwargs',nargs='*',action = KeyValue)\n\n#parsing arguments\nargs = parser.parse_args()\n\n# show the dictionary\nprint(args.kwargs)\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation\">Explanation<\/h4>\n\n\n\n<p>In the above example, we first created a class that inherits from the agrparse class where we write a similar kind of code as explained in example 2 to add elements in the dictionary. Then we initialized an object for argparse.Argparser to hold the values of arguments from CLI. Then we added that element in the parser variable, parsed the arguments and had it in the args variable. And then printed the values of the dictionary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-6-adding-key-value-pair-to-csv-file\"><span class=\"ez-toc-section\" id=\"Example_6_Adding_Key_Value_Pair_to_CSV_File\"><\/span>Example 6: Adding Key Value Pair to CSV File<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>As we can read a <span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/numpy-read-csv\/\" target=\"_blank\" rel=\"noreferrer noopener\">CSV file<\/a><\/strong><\/span> in the form of a dictionary, we can also write it in a CSV file. We can do it in the following way.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport csv\n\nwith open('names.csv', 'w', newline='') as csvfile:\n    fieldnames = &#x5B;'first_name', 'last_name']\n    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n\n    writer.writeheader()\n    writer.writerow({'first_name': 'Jane', 'last_name': 'Mary'})\n    writer.writerow({'first_name': 'Tom', 'last_name': ''})\n    writer.writerow({'first_name': '', 'last_name': 'Sasha'})\n<\/pre><\/div>\n\n\n<p> <strong>Output:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name.csv file:\nfirst_name,last_name\nJane,Mary\nTom,\n,Sasha<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-1\">Explanation<\/h4>\n\n\n\n<p>So, in the above example, we can see that we first created a CSV file using the &#8220;with&#8221; keyword. After that, we specified the field names that changed as the CSV file&#8217;s header. After that, we instantiate the object for <strong><a href=\"http:\/\/www.pythonpool.com\/python-csv-dictreader\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">dictwriter<\/span><\/a>,<\/strong> which contains several functions to write into the CSV file. Then, we used <strong>writeheader()<\/strong> and <strong>writer()<\/strong> function to write our key value pair into the CSV File.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-6-removing-key-value-pair-from-dictionary\"><span class=\"ez-toc-section\" id=\"Example_6_Removing_Key_Value_Pair_from_Dictionary\"><\/span>Example 6: Removing Key Value Pair from Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Creating Dictionary object\ndict_obj = {'a':'100', 'b':'200','c':'300','e':'400','f':'500','g':'600'} \nprint(&quot;dict_obj before delete &quot;, dict_obj) \n\n# Deleting element using del\ndel dict_obj&#x5B;'g']\nprint(&quot;dict_obj after delete using del &quot;, dict_obj)\n\n# Deleting element using pop method\ndict_obj.pop('f')\nprint(&quot;dict_obj after delete using pop method &quot;, dict_obj)\n<\/pre><\/div>\n\n\n<p> <strong>Output:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict_obj before delete  {'a': '100', 'b': '200', 'c': '300', 'e': '400', 'f': '500', 'g': '600'}\ndict_obj after delete using del  {'a': '100', 'b': '200', 'c': '300', 'e': '400', 'f': '500'}\ndict_obj after delete using pop method  {'a': '100', 'b': '200', 'c': '300', 'e': '400'}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-2\">Explanation<\/h4>\n\n\n\n<p>So, in the above example, we have seen how we can delete elements from dictionary objects. We have seen different methods of deleting elements by specifying keys inside the pop function or in the subscript of dict_obj while using the <a href=\"http:\/\/www.pythonpool.com\/python-del\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\"><strong>del keyword<\/strong><\/span><\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-7-key-value-pair-to-dataframe\"><span class=\"ez-toc-section\" id=\"Example_7_Key_Value_Pair_to_Dataframe\"><\/span>Example 7: Key Value Pair to Dataframe<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can also convert key value pairs into a dataframe table. To do that, we will use the following code. Let&#8217;s see that.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\n\nrecord1 = pd.Series({'i':'00','j':'01','k':'02','l':'03'})\nrecord2 = pd.Series({'i':'10','j':'11','k':'12','l':'13'})\nrecord3 = pd.Series({'i':'20','j':'21','k':'22','l':'23'})\nrecord4 = pd.Series({'i':'30','j':'31','k':'32','l':'33'})\n\n# Creating dataframe\ndf = pd.DataFrame(&#x5B;record1,record2,record3,record4],index = &#x5B;'0','1','2','3'])\nprint(df)\n<\/pre><\/div>\n\n\n<p> <strong>Output:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    i   j   k   l\n0  00  01  02  03\n1  10  11  12  13\n2  20  21  22  23\n3  30  31  32  33<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-3\">Explanation<\/h4>\n\n\n\n<p>We first need to convert a dictionary using the Series class to do that. After that, we passed the series objects into the <span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/pandas-to-csv\/\" target=\"_blank\" rel=\"noreferrer noopener\">DataFrame<\/a><\/strong><\/span> class as the argument. This will return a dataframe object which consists of a table of values we passed.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-beta monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-image\"><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/a><\/div><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs-on-python-key-value-pair\"><span class=\"ez-toc-section\" id=\"FAQs_on_Python_Key_Value_Pair\"><\/span>FAQs on Python Key Value Pair<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1641564074708\"><strong class=\"schema-faq-question\">Q1) How do you iterate key value pairs in python?<\/strong> <p class=\"schema-faq-answer\">To iterate over the key value pair, we will use the following code block.<br\/><br\/><code>#\u00a0Creating\u00a0Dictionary\u00a0object<br\/>dict_obj\u00a0=\u00a0{'a':'100',\u00a0'b':'200','c':'300','e':'400','f':'500','g':'600'}\u00a0<br\/>for\u00a0key,value\u00a0in\u00a0dict_obj.items():<br\/>\u00a0\u00a0\u00a0\u00a0print('{}\u00a0=\u00a0{}'.format(key,value))<\/code><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1641564192680\"><strong class=\"schema-faq-question\">Q2) How do you add a key value pair to a list in python?<\/strong> <p class=\"schema-faq-answer\">We can do that by using the list comprehension method. Use the following block of code to do that.<br\/><br\/><code>lst = [v for (k, v) in data.iteritems()]<\/code><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1641564474162\"><strong class=\"schema-faq-question\">Q3) How do I retrieve a key value pair within a listed dictionary in python?<\/strong> <p class=\"schema-faq-answer\">To retrieve a value from a list of dictionaries we will first specify the index position of the dictionary and then key of the dictionary whose value we want. For e.g.,<br\/><br\/><code>dict_obj\u00a0=\u00a0[{'a':'100',\u00a0'b':'200','c':'300','e':'400','f':'500','g':'600'},{'i':'00','j':'01','k':'02','l':'03'}]<br\/>print(dict_obj[0]['b'])<\/code><\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, today in this article, we learned about key value pairs. We have seen different ways of initializing dictionary objects statically and dynamically. We have also seen how we can update elements in dictionary objects using different methods. I hope this article has helped you. Thank You.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Python_Articles\"><\/span>Trending Python Articles<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>The ease that comes with the python programming language is one of the reasons why it is a programmer&#8217;s favorite. It is now handling data, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"7 Examples to Grasp Python Key Value Pair\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#more-18921\" aria-label=\"More on 7 Examples to Grasp Python Key Value Pair\">Read more<\/a><\/p>\n","protected":false},"author":25,"featured_media":18976,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4639,4637,4638,4635,4634],"class_list":["post-18921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-add-key-value-pair-to-dictionary-python","tag-key-value-pair-python","tag-python-dictionary-append-key-value-pair-2","tag-python-dictionary-remove-key-value-pair","tag-remove-key-value-pair-from-dictionary-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>7 Examples to Grasp Python Key Value Pair - Python Pool<\/title>\n<meta name=\"description\" content=\"In this article, we will learn about creating key value pair in python. We will see how can we create and update a dictionary object in python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"7 Examples to Grasp Python Key Value Pair\" \/>\n<meta property=\"og:description\" content=\"The ease that comes with the python programming language is one of the reasons why it is a programmer&#039;s favorite. It is now handling data, files,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-08T08:15:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-08T08:15:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Rishav Raj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rishav Raj\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\"},\"author\":{\"name\":\"Rishav Raj\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4\"},\"headline\":\"7 Examples to Grasp Python Key Value Pair\",\"datePublished\":\"2022-01-08T08:15:18+00:00\",\"dateModified\":\"2022-01-08T08:15:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\"},\"wordCount\":947,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp\",\"keywords\":[\"add key value pair to dictionary python\",\"key value pair python\",\"python dictionary append key value pair\",\"python dictionary remove key value pair\",\"remove key value pair from dictionary python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\",\"name\":\"7 Examples to Grasp Python Key Value Pair - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp\",\"datePublished\":\"2022-01-08T08:15:18+00:00\",\"dateModified\":\"2022-01-08T08:15:21+00:00\",\"description\":\"In this article, we will learn about creating key value pair in python. We will see how can we create and update a dictionary object in python\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564074708\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564192680\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564474162\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp\",\"width\":1200,\"height\":628,\"caption\":\"python key value pair\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"7 Examples to Grasp Python Key Value Pair\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4\",\"name\":\"Rishav Raj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"caption\":\"Rishav Raj\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564074708\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564074708\",\"name\":\"Q1) How do you iterate key value pairs in python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To iterate over the key value pair, we will use the following code block.<br\/><br\/>#\u00a0Creating\u00a0Dictionary\u00a0object<br\/>dict_obj\u00a0=\u00a0{'a':'100',\u00a0'b':'200','c':'300','e':'400','f':'500','g':'600'}\u00a0<br\/>for\u00a0key,value\u00a0in\u00a0dict_obj.items():<br\/>\u00a0\u00a0\u00a0\u00a0print('{}\u00a0=\u00a0{}'.format(key,value))\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564192680\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564192680\",\"name\":\"Q2) How do you add a key value pair to a list in python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"We can do that by using the list comprehension method. Use the following block of code to do that.<br\/><br\/>lst = [v for (k, v) in data.iteritems()]\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564474162\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564474162\",\"name\":\"Q3) How do I retrieve a key value pair within a listed dictionary in python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To retrieve a value from a list of dictionaries we will first specify the index position of the dictionary and then key of the dictionary whose value we want. For e.g.,<br\/><br\/>dict_obj\u00a0=\u00a0[{'a':'100',\u00a0'b':'200','c':'300','e':'400','f':'500','g':'600'},{'i':'00','j':'01','k':'02','l':'03'}]<br\/>print(dict_obj[0]['b'])\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"7 Examples to Grasp Python Key Value Pair - Python Pool","description":"In this article, we will learn about creating key value pair in python. We will see how can we create and update a dictionary object in python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-key-value-pair\/","og_locale":"en_US","og_type":"article","og_title":"7 Examples to Grasp Python Key Value Pair","og_description":"The ease that comes with the python programming language is one of the reasons why it is a programmer's favorite. It is now handling data, files,","og_url":"https:\/\/www.pythonpool.com\/python-key-value-pair\/","og_site_name":"Python Pool","article_published_time":"2022-01-08T08:15:18+00:00","article_modified_time":"2022-01-08T08:15:21+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp","type":"image\/webp"}],"author":"Rishav Raj","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Rishav Raj","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/"},"author":{"name":"Rishav Raj","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4"},"headline":"7 Examples to Grasp Python Key Value Pair","datePublished":"2022-01-08T08:15:18+00:00","dateModified":"2022-01-08T08:15:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/"},"wordCount":947,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp","keywords":["add key value pair to dictionary python","key value pair python","python dictionary append key value pair","python dictionary remove key value pair","remove key value pair from dictionary python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-key-value-pair\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/","url":"https:\/\/www.pythonpool.com\/python-key-value-pair\/","name":"7 Examples to Grasp Python Key Value Pair - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp","datePublished":"2022-01-08T08:15:18+00:00","dateModified":"2022-01-08T08:15:21+00:00","description":"In this article, we will learn about creating key value pair in python. We will see how can we create and update a dictionary object in python","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564074708"},{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564192680"},{"@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564474162"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-key-value-pair\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/01\/python-key-value-pair.webp","width":1200,"height":628,"caption":"python key value pair"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"7 Examples to Grasp Python Key Value Pair"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4","name":"Rishav Raj","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","caption":"Rishav Raj"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564074708","position":1,"url":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564074708","name":"Q1) How do you iterate key value pairs in python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"To iterate over the key value pair, we will use the following code block.<br\/><br\/>#\u00a0Creating\u00a0Dictionary\u00a0object<br\/>dict_obj\u00a0=\u00a0{'a':'100',\u00a0'b':'200','c':'300','e':'400','f':'500','g':'600'}\u00a0<br\/>for\u00a0key,value\u00a0in\u00a0dict_obj.items():<br\/>\u00a0\u00a0\u00a0\u00a0print('{}\u00a0=\u00a0{}'.format(key,value))","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564192680","position":2,"url":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564192680","name":"Q2) How do you add a key value pair to a list in python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"We can do that by using the list comprehension method. Use the following block of code to do that.<br\/><br\/>lst = [v for (k, v) in data.iteritems()]","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564474162","position":3,"url":"https:\/\/www.pythonpool.com\/python-key-value-pair\/#faq-question-1641564474162","name":"Q3) How do I retrieve a key value pair within a listed dictionary in python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"To retrieve a value from a list of dictionaries we will first specify the index position of the dictionary and then key of the dictionary whose value we want. For e.g.,<br\/><br\/>dict_obj\u00a0=\u00a0[{'a':'100',\u00a0'b':'200','c':'300','e':'400','f':'500','g':'600'},{'i':'00','j':'01','k':'02','l':'03'}]<br\/>print(dict_obj[0]['b'])","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=18921"}],"version-history":[{"count":15,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18921\/revisions"}],"predecessor-version":[{"id":18980,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18921\/revisions\/18980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/18976"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=18921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=18921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=18921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}