Introduction to Python Dictionary (With Examples)

Introduction to Python Dictionary (With Examples)

11 mins read874 Views Comment
clickHere
Updated on Jan 11, 2023 16:59 IST

In this tutorial, you will learn all about Dictionary in Python: creating dictionaries, accessing dictionary items, looping through a dictionary, and other dictionary operations with the help of examples.

2022_02_Python-Dictionary.jpg

Dictionaries are likely the most important and flexible mutable built-in data types in Python. In simple terms, a Python dictionary is a flexible sized arbitrary collection of key-value pairs, where key and value both are Python objects.

Example: Dict_1 = {‘first_name’: ‘Sam’, ‘age’: 4, ‘job’:’Professor’}

Explore- Python Online Courses & Certifications

Table of Contents

Creating Dictionaries

Dictionaries are similar to lists. Except that, if you think lists as ordered collections of objects, dictionaries can be thought of as unordered collections. In dictionaries, the items are stored and indexed by a key, instead of positional offset. Instead of positional offset, you specify a unique key for each value. The key is often the string type, but it can be any of Python’s immutable types such as boolean, float, integer, string, tuple, and others.

Must Read: lists in python

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 1: Creating Dictionaries\n
D1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: black">\n }\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: black">\n }\n
D3 \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n dict\n <span style="color: black">\n (name\n <span style="color: #66cc66">\n =\n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , age\n <span style="color: #66cc66">\n =\n <span style="color: #ff4500">\n 20\n <span style="color: #66cc66">\n , height \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 6.1\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "D1 is empty dictionary"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "creating 'dict' using curly braces and colons:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "creating 'dict' using dict built-in function:"\n <span style="color: #66cc66">\n , D3\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

As shown in the example, you can create a Python dictionary in a traditional way using curly brackets {} and colons, or using Python’s dict() function. One limitation to using dict() function is that the argument names that you are using need to be legal. Here’’ an example to showcase that:

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 2: Creating Dictionaries\n
D1 \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n dict\n <span style="color: black">\n (name\n <span style="color: #66cc66">\n =\n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , age \n <span style="color: #66cc66">\n =\n <span style="color: #ff4500">\n 20\n <span style="color: #66cc66">\n , height \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 6.1\n <span style="color: #66cc66">\n , \n <span style="color: #ff7700;font-weight:bold">\n def \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'professor'\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "creating 'dict' using dict built-in function:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n ) \n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Key Points About Dictionaries

Now that you have a basic idea of Python dictionary data type, let us look at main properties related to it.

Must Read: Data Type in Python

  • Python dictionary data types are the only built-in mapping type and they are more commonly known as hashes or associative arrays in other languages
  • You can perform same indexing operations as that of lists or tuples except that in dictionaries the indexes take form of a key, not a relative offset
  • Like lists, dictionaries are of variable-length, mutable and heterogeneous. You can change them in place and they can contain objects of any type. In the key-value pair, values can be a collection of mutable objects if needed, whereas the keys generally have to be immutable objects
  • You should note that dictionaries are mappings, not sequences. So, there is no notion of ordering among items and the sequence operations that you apply on lists, tuples, etc do not apply here

Access Dictionary Items

Dictionaries are often referred to as hashes or associative arrays. Like you already know, they associate each value with a key. So, you can fetch an item from the Python dictionary using the key with which you stored it. Unlike in lists, here the index takes the form of a key.

You can access the elements of the dictionary using keys, not offset position as dictionary items are inordered collection of arbitrary objects.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 3: Accessing dictionary items\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'first_name'\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'age'\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'job'\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D2\n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #print all the items\n
\n <span style="color: #808080;font-style: italic">\n #print(D2['company']) -> If the key isn’t in the dictionary, you will get a KeyError exception\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D2.\n <span style="color: black">\n get\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "company"\n <span style="color: black">\n )\n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #returns None if key is not present\n
\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

The first part of the code demonstrates how to access the items of the dictionary using the key values. If you try to use the key which is not part of the dictionary, Python will raise a KeyError exception. If you want to avoid getting the exception, you can use the get() method to fetch the values as shown in the last line of the example. In case the key that you use specify does not exist, the method will return None.

Getting Keys & Values

You can get a list of keys and a list of values using the keys() and values() method as shown below.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 4: keys() & values() method\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The list of keys:"\n <span style="color: #66cc66">\n , D2.\n <span style="color: black">\n keys\n <span style="color: black">\n (\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The list of values:"\n <span style="color: #66cc66">\n , D2.\n <span style="color: black">\n values\n <span style="color: black">\n (\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Add or Change Dictionary Items

Add Item to Dictionary

Adding elements to a Python dictionary is very easy. Just refer to the element that you want to add by its key and assign a value. Suppose if the key already exists, the value associated with it will be replaced and if the key is new, it will get added to the dictionary along with its value. The best thing is, you do not have to worry about Python throwing an IndexError exception.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 5: Adding dictionary items\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'XYZ'\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D2\n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #print all the items\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The newly added element:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'ABCDEFGH'\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The modified element:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Change the elements of Dictionary

Dictionaries are mutable just like lists, which is why you can change, expand, and shrink dictionaries in place.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 6: Changing dictionary items\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'ABC'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n ]\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Removing Dictionary Items

Delete an Item by Key with del

To delete a value from the dictionary, you need to delete the key associated with it. You can do that using the del keyword.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 7: Deleting dictionary items\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n del D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n ]\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n del D2\n
\n <span style="color: #808080;font-style: italic">\n #print(D2) -> Python gives error as the dictionary D2 no longer exists\n
\n
\n </span style="color: #808080;font-style: italic">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Delete all items using clear()

You can also delete all the values of the dictionary at once using the clear() method. This results in an empty dictionary unlike when you use del, where the dictionary itself is deleted.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 8: Deleting all dictionary items: using clear()\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
D2.\n <span style="color: black">\n clear\n <span style="color: black">\n (\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Looping Through a Dictionary

If you need to step through items in the dictionary, you can do so by iterating through the list of keys that you can get by using keys() method. Actually, you can step through a dictionary’s keys without having to call the keys() method as well. Both of these implementations are shown below.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 9: Iterating Through Dictionary Items: for loop\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n
\n <span style="color: #808080;font-style: italic">\n #using keys() method\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "With using keys()"\n <span style="color: black">\n )\n
ks \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n list\n <span style="color: black">\n (D2.\n <span style="color: black">\n keys\n <span style="color: black">\n (\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The list fo keys:"\n <span style="color: #66cc66">\n , ks\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n for keys \n <span style="color: #ff7700;font-weight:bold">\n in ks:\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (keys\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n '=>'\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n [keys\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n
\n <span style="color: #808080;font-style: italic">\n #without using keys() method\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "\n <span style="color: #000099;font-weight: bold"> Without using keys()"\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n for keys \n <span style="color: #ff7700;font-weight:bold">\n in D2:\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (keys\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n '=>'\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n [keys\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #000099;font-weight: bold">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

The net effect in this example is to print the unordered pairs of dictionary’s keys and values. Here’s another sample program for you. Try it out before checking the solution.

Problem Statement: Given a string, write a Python program that will count how many times each letter appears and print it as a dictionary with { letter: count} as key value pairs.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 10: Python program that will count how many times each letter appears\n
string1 \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'brontosaurus'\n
\n
\n <span style="color: #ff7700;font-weight:bold">\n def count_instances\n <span style="color: black">\n (s\n <span style="color: black">\n ):\n
D \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n dict\n <span style="color: black">\n (\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n for x \n <span style="color: #ff7700;font-weight:bold">\n in s:\n
\n <span style="color: #ff7700;font-weight:bold">\n if x \n <span style="color: #ff7700;font-weight:bold">\n not \n <span style="color: #ff7700;font-weight:bold">\n in D:\n
D\n <span style="color: black">\n [x\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 1\n
\n <span style="color: #ff7700;font-weight:bold">\n else:\n
D\n <span style="color: black">\n [x\n <span style="color: black">\n ] +\n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 1\n
\n
\n <span style="color: #ff7700;font-weight:bold">\n return D\n
\n
\n <span style="color: #ff7700;font-weight:bold">\n def print_dict\n <span style="color: black">\n (h\n <span style="color: black">\n ):\n
\n <span style="color: #ff7700;font-weight:bold">\n for c \n <span style="color: #ff7700;font-weight:bold">\n in h:\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (c\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n '=>'\n <span style="color: #66cc66">\n , h\n <span style="color: black">\n [c\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n
res_dictionary \n <span style="color: #66cc66">\n = count_instances\n <span style="color: black">\n (string1\n <span style="color: black">\n )\n
print_dict\n <span style="color: black">\n (res_dictionary\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

So, what’s happening in the count_instances function? The first line of the function basically creates an empty dictionary. Then the loop iterates through the string and counts the occurrences using the following code. Then we are calling a print_dict() method that loops through the resulting dictionary and prints key-value pairs. You can try to implement the same using comprehension. I suggest you try a few examples with a while loop as well!

Copy Dictionary

Let’s take an existing dictionary and assign it to a new one. Now try changing something from the original dictionary and see what happens to the new one.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 11: Copying Dictionary Elements\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
D_New \n <span style="color: #66cc66">\n = D2\n
D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'job'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'data analyst'\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (D_New\n <span style="color: black">\n )\n
\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

So, what happened? You will notice that if you make any change to the original dictionary, it will reflect in all the other names that refer to it as well. So to avoid this and make a perfect copy, you can use the copy() method as shown below.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 12: Copying Dictionary Elements using copy()\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
D_New \n <span style="color: #66cc66">\n = D2.\n <span style="color: #dc143c">\n copy\n <span style="color: black">\n (\n <span style="color: black">\n )\n
D2\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'job'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'data analyst'\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated D2 dictionary:"\n <span style="color: #66cc66">\n , D2\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The copied dictionary D_New:"\n <span style="color: #66cc66">\n , D_New\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #dc143c">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Joining Dictionaries

Merging Dictionaries Using update()

You can combine dictionaries using the update() method as shown below.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 13: Combining Dictionaries\n
D1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original D1 dictionary:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
D1.\n <span style="color: black">\n update\n <span style="color: black">\n (D2\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated D1 dictionary:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

You might be wondering what happens if the second dictionary has the same key as the original dictionary. Well the second dictionary wins. The value associated with that key in the original dictionary will be replaced.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 14: Combining Dictionaries: Secondary dict with same key as original dict\n
D1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data_analyst'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original D1 dictionary:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
D1.\n <span style="color: black">\n update\n <span style="color: black">\n (D2\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated D1 dictionary:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Merging Dictionaries using **

In the below example, we are creating a new dictionary which is resulting from merging of two dictionaries.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 15: Merging Dictionaries: using **\n
D1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n
D2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 28\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original D1 dictionary:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
D3 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {**D1\n <span style="color: #66cc66">\n ,**D2\n <span style="color: black">\n }\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated D1 dictionary:"\n <span style="color: #66cc66">\n , D3\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Again, you should notice that in case of the same keys in both the dictionaries, the second dictionary wins.

List of Dictionaries

In Python, you can have a list of dictionaries. Let’s learn how to create a list of dictionaries, and how to access its items.

Create a List of Dictionaries

You can easily create a list of dictionaries by enclosing the dictionaries in the square breakers. As simple as that.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 16: Creating List of Dictionaries\n
list_of_dictionaries \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n <span style="color: #66cc66">\n , \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data analyst'\n <span style="color: black">\n }\n <span style="color: black">\n ]\n
\n
\n <span style="color: #808080;font-style: italic">\n #print the list\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (list_of_dictionaries\n <span style="color: black">\n )\n
\n
\n <span style="color: #808080;font-style: italic">\n #validating the elements\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #008000">\n type\n <span style="color: black">\n (list_of_dictionaries\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #008000">\n type\n <span style="color: black">\n (list_of_dictionaries\n <span style="color: black">\n [\n <span style="color: #ff4500">\n 0\n <span style="color: black">\n ]\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Appending a new dictionary to list of dictionaries

You can append a new dictionary to the list using the append() method as shown below.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 17: Appending a new dict to list of dictionaries using append()\n
list_of_dict \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n <span style="color: #66cc66">\n , \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data analyst'\n <span style="color: black">\n }\n <span style="color: black">\n ]\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The original list:"\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n )\n
new_dict \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Dean'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data scientist'\n <span style="color: black">\n }\n
\n
\n <span style="color: #808080;font-style: italic">\n #Appending the list of dictionaries with the new dictionary using append() method\n
list_of_dict.\n <span style="color: black">\n append\n <span style="color: black">\n (new_dict\n <span style="color: black">\n )\n
\n
\n <span style="color: #808080;font-style: italic">\n #print the new list\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The updated list:"\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Accessing the key-value pairs from the list of dictionaries

Now that’s simple. You have already learnt how to access the elements of the list. You can do the same when the elements of the list are dictionaries as well.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 18: Accessing key-value pairs\n
list_of_dict \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n <span style="color: #66cc66">\n , \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data analyst'\n <span style="color: black">\n }\n <span style="color: black">\n ]\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The given list of dictionaries:"\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n )\n
\n
\n <span style="color: #808080;font-style: italic">\n #Accessing key value pairs using indexing - print the last element\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The key value pairs are:"\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n [-\n <span style="color: #ff4500">\n 1\n <span style="color: black">\n ]\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Updating the key-value pairs of list of dictionaries

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 19: Updating key-value pairs\n
list_of_dict \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n <span style="color: #66cc66">\n , \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data analyst'\n <span style="color: black">\n }\n <span style="color: black">\n ]\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The given list of dictionaries:"\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n )\n
\n
\n <span style="color: #808080;font-style: italic">\n #Adding a new key-value pair to the 1st dictionary of the list\n
list_of_dict\n <span style="color: black">\n [\n <span style="color: #ff4500">\n 0\n <span style="color: black">\n ]\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'age'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 27\n
\n
\n <span style="color: #808080;font-style: italic">\n #updating the existing key of 2nd dictionary of the list\n
list_of_dict\n <span style="color: black">\n [\n <span style="color: #ff4500">\n 1\n <span style="color: black">\n ]\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'first_name'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'dean'\n
\n
\n <span style="color: #808080;font-style: italic">\n #deleting the key pair of 1st dictionary\n
\n <span style="color: #ff7700;font-weight:bold">\n del list_of_dict\n <span style="color: black">\n [\n <span style="color: #ff4500">\n 0\n <span style="color: black">\n ]\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'job'\n <span style="color: black">\n ]\n
\n
\n <span style="color: #808080;font-style: italic">\n #printing the updated list\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The key value pairs are:"\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: black">\n </span style="color: #808080;font-style: italic">\n </span style="color: #ff4500">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff4500">\n </span style="color: black">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

While the code may seem a little intimidating. It’s actually not. You are just accessing the elements of the list like you usually do and then playing with the key-value pairs of respective dictionaries.

Search a List of Dictionaries in Python

There are quite a few methods that you can use to search a list of dictionaries.

  1. next() function
  2. filter() function

Using next() function

The next() basically returns the next item in the iterator. By implementing a for loop along with the next() function, you can search a list of dictionaries for a particular value.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 20: Searching a list of dictionaries\n
list_of_dict \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n
\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n <span style="color: #66cc66">\n , \n
\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data analyst'\n <span style="color: black">\n }\n
\n <span style="color: black">\n ]\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (next\n <span style="color: black">\n (i \n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in list_of_dict \n <span style="color: #ff7700;font-weight:bold">\n if i\n <span style="color: black">\n [\n <span style="color: #483d8b">\n "first_name"\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n == \n <span style="color: #483d8b">\n "Sammy"\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (next\n <span style="color: black">\n (\n <span style="color: black">\n (i \n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in list_of_dict \n <span style="color: #ff7700;font-weight:bold">\n if i\n <span style="color: black">\n [\n <span style="color: #483d8b">\n "job"\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n == \n <span style="color: #483d8b">\n "python"\n <span style="color: black">\n )\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n "Not Present"\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

The last line will throw an error, so I tweaked it a little to return a default message if the search value is not present

Using filter() function

The filter(function, sequence) basically compares the ‘function’ argument with each element of the ‘sequence’. The filter() returns an object of filter class, which we can convert to a list with the list() method.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 21: Searching a list of dictionaries: Using filter()\n
list_of_dict \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [\n
\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n ,\n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: black">\n }\n <span style="color: #66cc66">\n , \n
\n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Sammy'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'data analyst'\n <span style="color: black">\n }\n
\n <span style="color: black">\n ]\n
\n <span style="color: #008000">\n list\n <span style="color: black">\n (\n <span style="color: #008000">\n filter\n <span style="color: black">\n (\n <span style="color: #ff7700;font-weight:bold">\n lambda item: item\n <span style="color: black">\n [\n <span style="color: #483d8b">\n 'first_name'\n <span style="color: black">\n ] \n <span style="color: #66cc66">\n == \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , list_of_dict\n <span style="color: black">\n )\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: black">\n </span style="color: #008000">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Also Read: Top Python Interview Questions and Answers

Dictionary Methods

We have already used a lot of dictionary methods such as items(), keys(), dict(), update(), clear(), copy(0, values(), etc. Let us explore a few more.

 
\n <pre class="python" style="font-family:monospace">\n <span style="color: #808080;font-style: italic">\n #Example 22: Searching a list of dictionaries: Using filter()\n
D1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n {\n <span style="color: #483d8b">\n 'first_name' : \n <span style="color: #483d8b">\n 'Jim'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'age' : \n <span style="color: #ff4500">\n 23\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'height' : \n <span style="color: #ff4500">\n 6.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'job' : \n <span style="color: #483d8b">\n 'developer'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'company': \n <span style="color: #483d8b">\n 'XYZ'\n <span style="color: black">\n }\n
\n
\n <span style="color: #808080;font-style: italic">\n #fetch a value of dict by its key using get(key, default)\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "Is key named 'name' present?"\n <span style="color: #66cc66">\n , D1.\n <span style="color: black">\n get(\n <span style="color: #483d8b">\n 'name'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'not present'\n <span style="color: black">\n ))\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "Is key named 'first_name' present?"\n <span style="color: #66cc66">\n , D1.\n <span style="color: black">\n get(\n <span style="color: #483d8b">\n 'first_name'\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'not present'\n <span style="color: black">\n ))\n
\n
\n <span style="color: #808080;font-style: italic">\n #remove an items based on key using pop(key, default)\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "Original Dict:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
D1.\n <span style="color: black">\n pop(\n <span style="color: #483d8b">\n 'company'\n <span style="color: black">\n )\n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "Updated Dict:"\n <span style="color: #66cc66">\n , D1\n <span style="color: black">\n )\n
\n
\n <span style="color: #808080;font-style: italic">\n #avoiding KeyError exceptions using setdefault(key, value)\n
\n <span style="color: #808080;font-style: italic">\n #key is already present, so display associated value\n
x \n <span style="color: #66cc66">\n = D1.\n <span style="color: black">\n setdefault(\n <span style="color: #483d8b">\n "age"\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n "27"\n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The value associated with key 'age':"\n <span style="color: #66cc66">\n , x\n <span style="color: black">\n )\n
\n <span style="color: #808080;font-style: italic">\n #key is not present, so adds the value & displays it\n
y \n <span style="color: #66cc66">\n = D1.\n <span style="color: black">\n setdefault(\n <span style="color: #483d8b">\n "company"\n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n "xyz"\n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print\n <span style="color: black">\n (\n <span style="color: #483d8b">\n "The value associated with key 'company':"\n <span style="color: #66cc66">\n , y\n <span style="color: black">\n )\n
\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #ff7700;font-weight:bold">\n </span style="color: #808080;font-style: italic">\n </span style="color: black">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #ff4500">\n </span style="color: #483d8b">\n </span style="color: #66cc66">\n </span style="color: #483d8b">\n </span style="color: #483d8b">\n </span style="color: black">\n </span style="color: #66cc66">\n </span style="color: #808080;font-style: italic">\n </pre class="python" style="font-family:monospace">
Copy code

Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.

Click here to submit its review.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments