{"id":445986,"date":"2020-01-07T11:42:51","date_gmt":"2020-01-07T06:12:51","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=74750"},"modified":"2020-01-07T11:42:51","modified_gmt":"2020-01-07T06:12:51","slug":"python-dictionaries-2","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/","title":{"rendered":"Python Dictionaries &#8211; A collection of key-value pairs"},"content":{"rendered":"<p>After strings and lists, let\u2019s talk about dictionaries.<\/p>\n<p>Like a real-life dictionary has words and meanings, Python dictionaries have <strong>keys<\/strong> and <strong>values<\/strong>. They are an important data structure in Python and today, we will learn how to <strong>create<\/strong>, <strong>access<\/strong>, <strong>manipulate<\/strong> and <strong>use<\/strong> them.<\/p>\n<h3>What are Dictionaries in Python?<\/h3>\n<p>Dictionaries in Python are <strong>collections<\/strong> that are <strong>unordered<\/strong>, <strong>indexed<\/strong> and <strong>mutable<\/strong>. They hold <strong>keys<\/strong> and <strong>values<\/strong>. This is a dictionary:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; animals={'dog': 'mammal', 'cat': 'mammal', 'snake': 'reptile', 'frog': 'amphibian'}<\/pre>\n<p>Dictionaries are in <strong>curly brackets<\/strong>.<\/p>\n<p><strong>Key-value<\/strong> <strong>pairs<\/strong> are separated by <strong>commas<\/strong> and <strong>keys<\/strong> and <strong>values<\/strong> are separated by <strong>colons<\/strong>. Keys in dictionaries are <strong>unique<\/strong> and <strong>immutable<\/strong>.<\/p>\n<p>Unlike other <strong>data structures<\/strong>, dictionaries hold <strong>two items<\/strong> in <strong>pairs<\/strong> instead of a <strong>single item<\/strong>. This is like a real-life dictionary. You can search for a <strong>value<\/strong> if you know the <strong>key<\/strong>. <strong>One key<\/strong> cannot have <strong>two values<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; dict1={1:4, 1:9, 2:7}\n&gt;&gt;&gt; dict1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 9, 2: 7}<\/div>\n<p>The value of 1 is 9, not 4.<\/p>\n<h3>Creating Dictionaries in Python<\/h3>\n<p>There are many ways to create a <strong>dictionary<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares={1:1, 2:4, 3:9}\n\n&gt;&gt;&gt; squares={}\n&gt;&gt;&gt; squares[1]=1\n&gt;&gt;&gt; squares[2]=4\n&gt;&gt;&gt; squares[3]=9\n&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 4, 3: 9}<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares=dict()\n&gt;&gt;&gt; squares[1]=1\n&gt;&gt;&gt; squares[2]=4\n&gt;&gt;&gt; squares[3]=9\n&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 4, 3: 9}<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares={num:num**2 for num in range(1,4)}\n&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 4, 3: 9}<\/div>\n<h3>Accessing Items in Python Dictionaries<\/h3>\n<p>After you have defined a dictionary, how do you access its items? There are two ways to do this- <strong>indexing<\/strong> and the<strong> get() method<\/strong>.<\/p>\n<h4>1. Indexing<\/h4>\n<p>Dictionaries are <strong>unordered<\/strong> but we can <strong>index<\/strong> them with their <strong>keys<\/strong>.<\/p>\n<p>Let\u2019s take an example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 4, 3: 9}<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares[2]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares={'one':1, 'two':4, 'three':9}\n&gt;&gt;&gt; squares['two']<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<h4>2. get()<\/h4>\n<p>The <strong>get() method<\/strong> does the same thing as <strong>indexing<\/strong>. It takes a <strong>parameter<\/strong> for the <strong>key<\/strong> for which we want to find the <strong>value<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares.get('one')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>If the key <strong>does not exist<\/strong> in the dictionary, it returns <strong>None<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares.get('four')\n&gt;&gt;&gt;<\/pre>\n<h3>Adding Items in Python Dictionaries<\/h3>\n<p>If you have a dictionary (<strong>empty or non-empty<\/strong>) and want to <strong>add<\/strong> more <strong>key-value<\/strong> <strong>pairs<\/strong> to it, you can:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{&#8216;one&#8217;: 1, &#8216;two&#8217;: 4, &#8216;three&#8217;: 9}<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares['four']=16\n&gt;&gt;&gt; squares['five']=25\n&gt;&gt;&gt; squares['six']=36<\/pre>\n<p><strong>squares<\/strong> is this dictionary now:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{&#8216;one&#8217;: 1, &#8216;two&#8217;: 4, &#8216;three&#8217;: 9, &#8216;four&#8217;: 16, &#8216;five&#8217;: 25, &#8216;six&#8217;: 36}<\/div>\n<h3>Removing Items in Python Dictionaries<\/h3>\n<p>Dictionaries are <strong>mutable<\/strong> and so we can <strong>remove items<\/strong> from them.<\/p>\n<p>Let\u2019s look at different ways to do this:<\/p>\n<h4>1. pop()<\/h4>\n<p>The <strong>pop() method<\/strong> removes the item with the key we <strong>specify<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares={1:1, 2:4, 3:9, 4:16, 5:25, 6:36, 7:49, 8:64, 9:81, 10:100}\n&gt;&gt;&gt; squares.pop(7)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">49<\/div>\n<p>If the key is <strong>not present<\/strong> in the dictionary, it raises a <strong>KeyError<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares.pop(11)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#32&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>squares.pop(11)<br \/>\nKeyError: 11<\/div>\n<h4>2. popitem()<\/h4>\n<p>This method <strong>removes<\/strong> the <strong>last inserted<\/strong> <strong>item<\/strong> from the dictionary and <strong>returns<\/strong> it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares.popitem()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(10, 100)<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 8: 64, 9: 81}<\/div>\n<h4>3. del<\/h4>\n<p>We can use the <strong>del keyword<\/strong> to delete dictionary items too.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del squares[4]\n&gt;&gt;&gt; squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 4, 3: 9, 5: 25, 6: 36, 8: 64, 9: 81}<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del squares[10]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#37&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>del squares[10]<br \/>\nKeyError: 10<\/div>\n<p>You can also delete the <strong>complete<\/strong> dictionary with this <strong>del keyword<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={1:1, 2:8, 3:27, 4:64}\n&gt;&gt;&gt; del cubes\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#41&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>cubes<br \/>\nNameError: name &#8216;cubes&#8217; is not defined<\/div>\n<h4>4. clear()<\/h4>\n<p>The <strong>clear() method<\/strong> does not delete a dictionary, it <strong>removes<\/strong> all the<strong> key-value pairs<\/strong> from it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={1:1, 2:8, 3:27, 4:64}\n&gt;&gt;&gt; cubes.clear()\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{}<\/div>\n<h3>Changing Values in Python Dictionaries<\/h3>\n<p>Again, dictionaries are <strong>mutable<\/strong> and you can <strong>change values<\/strong> for different keys.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={1:1, 2:8, 3:27, 4:64}\n&gt;&gt;&gt; cubes[3]=27.0\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 3: 27.0, 4: 64}<\/div>\n<p>In this example, we change the value of key 3 to 27.0.<\/p>\n<h3>Looping on Dictionaries<\/h3>\n<p>Like <strong>strings <\/strong>and <strong>lists<\/strong>, we can <strong>loop<\/strong> on <strong>dictionaries<\/strong>. If you want to perform an operation for<strong> all values<\/strong> in a dictionary, you can use it in a <strong>for loop<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for key,value in squares.items():\n    print(key,value)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1 1<br \/>\n2 4<br \/>\n3 9<br \/>\n5 25<br \/>\n6 36<br \/>\n8 64<br \/>\n9 81<\/div>\n<p>We use the <strong>items() method<\/strong> for this to get the <strong>key-value pairs<\/strong> for this dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for key in squares:\n    print(key)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n5<br \/>\n6<br \/>\n8<br \/>\n9<\/div>\n<p>These are the <strong>keys<\/strong> in the dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for value in squares.values():\n    print(value)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n4<br \/>\n9<br \/>\n25<br \/>\n36<br \/>\n64<br \/>\n81<\/div>\n<p>And these are the <strong>values<\/strong> in this dictionary.<\/p>\n<h3>Length and Membership<\/h3>\n<p>We can check a dictionary\u2019s <strong>length<\/strong> with the <strong>len() function<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(squares)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>If you want to see whether a key is <strong>present<\/strong> in a dictionary, you can use the <strong>membership operators<\/strong> <strong>in<\/strong> and <strong>not in<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7 in squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 8 in squares<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h3>Copying Dictionaries in Python<\/h3>\n<p>You can copy a dictionary in two ways in Python &#8211; using <strong>copy()<\/strong> and using <strong>dict()<\/strong>. You <strong>cannot copy<\/strong> a dictionary by assigning it to a <strong>new variable<\/strong>. That <strong>creates<\/strong> a <strong>reference<\/strong> to the <strong>original dictionary<\/strong>, not a copy.<\/p>\n<p>You can copy a dictionary in two ways &#8211; <strong>copy()<\/strong> and <strong>dict()<\/strong>.<\/p>\n<h4>1. copy()<\/h4>\n<p><strong>copy()<\/strong> is an<strong> in-built<\/strong> method which creates a <strong>shallow copy<\/strong> of a <strong>dictionary<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 3: 27.0, 4: 64}<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d=cubes.copy()\n&gt;&gt;&gt; d[3]=27\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 3: 27.0, 4: 64}<\/div>\n<h4>2. dict()<\/h4>\n<p><strong>dict()<\/strong> creates a dictionary from a <strong>different data type<\/strong> or even from a <strong>dictionary<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; dict([(1,2),(2,4)])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 2, 2: 4}<\/div>\n<p>Doing this <strong>without parameters<\/strong> gives you an <strong>empty dictionary<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; dict()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{}<\/div>\n<h3>Nested Dictionaries and Comprehension<\/h3>\n<p>Dictionaries can hold <strong>any number<\/strong> of dictionaries.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; groceries={\n  'dairy':{\n    'milk':20,\n    'eggs':15},\n  'breads':{\n    'bread':10}\n  }<\/pre>\n<p>This is a <strong>nested dictionary<\/strong>.<\/p>\n<p>The groceries dictionary has the dairy and breads dictionaries in it.<\/p>\n<p><strong>Dictionary Comprehension &#8211; <\/strong>Like list comprehension, you can use <strong>dictionary comprehension<\/strong> to<strong> quickly create dictionaries<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={val:val**3 for val in range(6)}\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125}<\/div>\n<p>This dictionary has the <strong>cubes<\/strong> of numbers.<\/p>\n<h3>Python Dictionary Methods<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-dictionary-methods.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75041 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-dictionary-methods.jpg\" alt=\"python dictionaries method\" width=\"684\" height=\"424\" \/><\/a><\/h3>\n<p>Python has some <strong>built-in<\/strong> methods and <strong>functions<\/strong> for all data structures. Let\u2019s see what methods we can call on dictionaries.<\/p>\n<h4>1. clear()<\/h4>\n<p>clear() <strong>removes<\/strong> all the<strong> key-value<\/strong> <strong>pairs<\/strong> of a dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={1:1, 2:8, 3:27}\n&gt;&gt;&gt; cubes.clear()\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{}<\/div>\n<h4>2. copy()<\/h4>\n<p>copy() <strong>returns<\/strong> a <strong>shallow copy<\/strong> of a <strong>dictionary<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={1:1, 2:8, 3:27}\n&gt;&gt;&gt; cubes1=cubes.copy()\n&gt;&gt;&gt; cubes1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 3: 27}<\/div>\n<h4>3. fromkeys()<\/h4>\n<p>This method takes <strong>keys<\/strong> from an <strong>iterable<\/strong> and <strong>creates<\/strong> a <strong>new dictionary<\/strong> from it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes1=dict.fromkeys([1,2,3],10)\n&gt;&gt;&gt; cubes1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 10, 2: 10, 3: 10}<\/div>\n<h4>4. get()<\/h4>\n<p>This method gets the <strong>value<\/strong> for a <strong>key<\/strong>. The <strong>default<\/strong> value is <strong>None<\/strong>. And it raises a <strong>KeyError<\/strong> if the key is <strong>not present<\/strong> in the dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.get(3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">27<\/div>\n<h4>5. items()<\/h4>\n<p>This method returns a list of a dictionary\u2019s <strong>key-value pairs<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.items()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">dict_items([(1, 1), (2, 8), (3, 27)])<\/div>\n<p>You can use this to loop on a dictionary.<\/p>\n<h4>6. keys()<\/h4>\n<p>This method returns a <strong>list<\/strong> of the <strong>key\u2019s dictionaries<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.keys()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">dict_keys([1, 2, 3])<\/div>\n<h4>7. setdefault()<\/h4>\n<p>This is like <strong>get()<\/strong> but adds a <strong>key-value pair<\/strong> to a <strong>dictionary<\/strong> if it is <strong>not in<\/strong> the <strong>dictionary<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.setdefault(4, 10)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">10<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 3: 27, 4: 10}<\/div>\n<h4>8. update()<\/h4>\n<p>update() <strong>adds key-value pairs<\/strong> from <strong>one dict<\/strong> to <strong>another dict<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes={1: 1, 2: 8, 3: 27}\n&gt;&gt;&gt; cubes1={4:64, 5:125}\n&gt;&gt;&gt; cubes.update(cubes1)\n&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 3: 27, 4: 64, 5: 125}<\/div>\n<p><strong>cubes<\/strong> now has the <strong>key-value pairs<\/strong> from <strong>cubes1<\/strong> as well.<\/p>\n<h4>9. values()<\/h4>\n<p>values() <strong>returns<\/strong> a <strong>list<\/strong> of the <strong>values<\/strong> in the dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.values()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">dict_values([1, 8, 27, 64, 125])<\/div>\n<h4>10. pop()<\/h4>\n<p>This <strong>removes<\/strong> the <strong>given key<\/strong> from a dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.pop(3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">27<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 4: 64, 5: 125}<\/div>\n<h4>11. popitem()<\/h4>\n<p>This <strong>removes<\/strong> the <strong>last <\/strong>added <strong>key-value pair<\/strong> from a dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes.popitem()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(5, 125)<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cubes<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: 1, 2: 8, 4: 64}<\/div>\n<h3>Python Dictionary Functions<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-dictionary-functions.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75042 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-dictionary-functions.jpg\" alt=\"python dictionaries function\" width=\"630\" height=\"422\" \/><\/a><\/h3>\n<p>Dictionaries also have some <strong>built-in functions<\/strong>.<\/p>\n<p>Let\u2019s discuss them.<\/p>\n<h4>1. all()<\/h4>\n<p>This returns <strong>True<\/strong> if <strong>all keys<\/strong> in a dictionary are <strong>True<\/strong> or if the dictionary is <strong>empty<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all(cubes)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all({})<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>2. any()<\/h4>\n<p>This returns <strong>True<\/strong> if at <strong>least one key<\/strong> in a dictionary is <strong>True<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; any({1:1, False: True})<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>3. len()<\/h4>\n<p>This returns the <strong>length<\/strong> of a dictionary.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(cubes)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<h4>4. sorted()<\/h4>\n<p>This returns a <strong>sorted version<\/strong> of a <strong>dictionary\u2019s keys<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sorted(cubes)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4]<\/div>\n<h3>Summary<\/h3>\n<p>Dictionaries are Python&#8217;s implementation of a knowledge structure that&#8217;s more generally referred to as an\u00a0<strong>associative array<\/strong>. A dictionary consists of a set of\u00a0<strong>key-value pairs<\/strong>.<\/p>\n<p>We learned about <strong>creating<\/strong> Python <strong>dictionaries<\/strong>, <strong>accessing<\/strong>, <strong>adding<\/strong>, <strong>removing<\/strong>, <strong>changing<\/strong>, <strong>looping<\/strong>, <strong>length<\/strong>, <strong>membership<\/strong>, <strong>copying<\/strong>, <strong>nesting<\/strong>, <strong>comprehension<\/strong>, <strong>methods<\/strong> and <strong>dictionary functions<\/strong>.<\/p>\n<p>Now you know what are dictionaries in Python, how to\u00a0<strong>use<\/strong>\u00a0and\u00a0<strong>manipulate<\/strong>\u00a0them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After strings and lists, let\u2019s talk about dictionaries. Like a real-life dictionary has words and meanings, Python dictionaries have keys and values. They are an important data structure in Python and today, we will&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75024,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1195,1196,1164,1197,1198,1200,1202],"class_list":["post-445986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-creating-dictionaries-in-python","tag-dictionaries-in-python","tag-python-dictionaries","tag-python-dictionary-functions","tag-python-dictionary-methods","tag-what-are-dictionaries-in-python","tag-what-are-python-dictionaries"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Dictionaries - A collection of key-value pairs - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Dictionaries - Learn to create, access, use and manipulate dictionaries in Python. Also, learn about its methods and functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Dictionaries - A collection of key-value pairs - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Dictionaries - Learn to create, access, use and manipulate dictionaries in Python. Also, learn about its methods and functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-07T06:12:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-dictionaries.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Dictionaries - A collection of key-value pairs - TechVidvan","description":"Python Dictionaries - Learn to create, access, use and manipulate dictionaries in Python. Also, learn about its methods and functions.","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:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/","og_locale":"en_US","og_type":"article","og_title":"Python Dictionaries - A collection of key-value pairs - TechVidvan","og_description":"Python Dictionaries - Learn to create, access, use and manipulate dictionaries in Python. Also, learn about its methods and functions.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-07T06:12:51+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-dictionaries.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Dictionaries &#8211; A collection of key-value pairs","datePublished":"2020-01-07T06:12:51+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/"},"wordCount":1137,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-dictionaries.jpg","keywords":["Creating Dictionaries in python","dictionaries in python","python dictionaries","python dictionary functions","python dictionary methods","what are dictionaries in python","what are python dictionaries"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/","url":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/","name":"Python Dictionaries - A collection of key-value pairs - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-dictionaries.jpg","datePublished":"2020-01-07T06:12:51+00:00","description":"Python Dictionaries - Learn to create, access, use and manipulate dictionaries in Python. Also, learn about its methods and functions.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-dictionaries.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-dictionaries.jpg","width":802,"height":420,"caption":"dictionaries in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-dictionaries-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Dictionaries &#8211; A collection of key-value pairs"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/445986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=445986"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/445986\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75024"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=445986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=445986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=445986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}