Introduction to Python Dictionary (With Examples)
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.
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
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
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 1: Creating DictionariesD1 <span style="color: #66cc66">= <span style="color: black">{<span style="color: black">}D2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: black">}D3 <span style="color: #66cc66">= <span style="color: #008000">dict<span style="color: black">(name<span style="color: #66cc66">=<span style="color: #483d8b">'Jim'<span style="color: #66cc66">, age<span style="color: #66cc66">=<span style="color: #ff4500">20<span style="color: #66cc66">, height <span style="color: #66cc66">= <span style="color: #ff4500">6.1<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"D1 is empty dictionary"<span style="color: #66cc66">, D1<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"creating 'dict' using curly braces and colons:"<span style="color: #66cc66">, D2<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"creating 'dict' using dict built-in function:"<span style="color: #66cc66">, D3<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #008000"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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:
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 2: Creating DictionariesD1 <span style="color: #66cc66">= <span style="color: #008000">dict<span style="color: black">(name<span style="color: #66cc66">=<span style="color: #483d8b">'Jim'<span style="color: #66cc66">, age <span style="color: #66cc66">=<span style="color: #ff4500">20<span style="color: #66cc66">, height <span style="color: #66cc66">= <span style="color: #ff4500">6.1<span style="color: #66cc66">, <span style="color: #ff7700;font-weight:bold">def <span style="color: #66cc66">= <span style="color: #483d8b">'professor'<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"creating 'dict' using dict built-in function:"<span style="color: #66cc66">, D1<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff7700;font-weight:bold"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #008000"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 3: Accessing dictionary itemsD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D2<span style="color: black">[<span style="color: #483d8b">'first_name'<span style="color: black">]<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D2<span style="color: black">[<span style="color: #483d8b">'age'<span style="color: black">]<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D2<span style="color: black">[<span style="color: #483d8b">'job'<span style="color: black">]<span style="color: black">) <span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D2<span style="color: black">) <span style="color: #808080;font-style: italic">#print all the items<span style="color: #808080;font-style: italic">#print(D2['company']) -> If the key isn’t in the dictionary, you will get a KeyError exception<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D2.<span style="color: black">get<span style="color: black">(<span style="color: #483d8b">"company"<span style="color: black">)<span style="color: black">) <span style="color: #808080;font-style: italic">#returns None if key is not present </span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 4: keys() & values() methodD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The list of keys:"<span style="color: #66cc66">, D2.<span style="color: black">keys<span style="color: black">(<span style="color: black">)<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The list of values:"<span style="color: #66cc66">, D2.<span style="color: black">values<span style="color: black">(<span style="color: black">)<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 5: Adding dictionary itemsD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}D2<span style="color: black">[<span style="color: #483d8b">'company'<span style="color: black">] <span style="color: #66cc66">= <span style="color: #483d8b">'XYZ'<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D2<span style="color: black">) <span style="color: #808080;font-style: italic">#print all the items<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The newly added element:"<span style="color: #66cc66">, D2<span style="color: black">[<span style="color: #483d8b">'company'<span style="color: black">]<span style="color: black">)D2<span style="color: black">[<span style="color: #483d8b">'company'<span style="color: black">] <span style="color: #66cc66">= <span style="color: #483d8b">'ABCDEFGH'<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The modified element:"<span style="color: #66cc66">, D2<span style="color: black">[<span style="color: #483d8b">'company'<span style="color: black">]<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Change the elements of Dictionary
Dictionaries are mutable just like lists, which is why you can change, expand, and shrink dictionaries in place.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 6: Changing dictionary itemsD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original dictionary:"<span style="color: #66cc66">, D2<span style="color: black">)D2<span style="color: black">[<span style="color: #483d8b">'company'<span style="color: black">] <span style="color: #66cc66">= <span style="color: black">[<span style="color: #483d8b">'ABC'<span style="color: #66cc66">, <span style="color: #483d8b">'XYZ'<span style="color: black">]<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated dictionary:"<span style="color: #66cc66">, D2<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 7: Deleting dictionary itemsD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original dictionary:"<span style="color: #66cc66">, D2<span style="color: black">)<span style="color: #ff7700;font-weight:bold">del D2<span style="color: black">[<span style="color: #483d8b">'company'<span style="color: black">]<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated dictionary:"<span style="color: #66cc66">, D2<span style="color: black">)<span style="color: #ff7700;font-weight:bold">del D2<span style="color: #808080;font-style: italic">#print(D2) -> Python gives error as the dictionary D2 no longer exists </span style="color: #808080;font-style: italic"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 8: Deleting all dictionary items: using clear()D2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original dictionary:"<span style="color: #66cc66">, D2<span style="color: black">)D2.<span style="color: black">clear<span style="color: black">(<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated dictionary:"<span style="color: #66cc66">, D2<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 9: Iterating Through Dictionary Items: for loopD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">} <span style="color: #808080;font-style: italic">#using keys() method<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"With using keys()"<span style="color: black">)ks <span style="color: #66cc66">= <span style="color: #008000">list<span style="color: black">(D2.<span style="color: black">keys<span style="color: black">(<span style="color: black">)<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The list fo keys:"<span style="color: #66cc66">, ks<span style="color: black">)<span style="color: #ff7700;font-weight:bold">for keys <span style="color: #ff7700;font-weight:bold">in ks: <span style="color: #ff7700;font-weight:bold">print<span style="color: black">(keys<span style="color: #66cc66">, <span style="color: #483d8b">'=>'<span style="color: #66cc66">, D2<span style="color: black">[keys<span style="color: black">]<span style="color: black">) <span style="color: #808080;font-style: italic">#without using keys() method<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"<span style="color: #000099;font-weight: bold">\nWithout using keys()"<span style="color: black">)<span style="color: #ff7700;font-weight:bold">for keys <span style="color: #ff7700;font-weight:bold">in D2: <span style="color: #ff7700;font-weight:bold">print<span style="color: black">(keys<span style="color: #66cc66">, <span style="color: #483d8b">'=>'<span style="color: #66cc66">, D2<span style="color: black">[keys<span style="color: black">]<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #000099;font-weight: bold"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #008000"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 10: Python program that will count how many times each letter appearsstring1 <span style="color: #66cc66">= <span style="color: #483d8b">'brontosaurus' <span style="color: #ff7700;font-weight:bold">def count_instances<span style="color: black">(s<span style="color: black">): D <span style="color: #66cc66">= <span style="color: #008000">dict<span style="color: black">(<span style="color: black">) <span style="color: #ff7700;font-weight:bold">for x <span style="color: #ff7700;font-weight:bold">in s: <span style="color: #ff7700;font-weight:bold">if x <span style="color: #ff7700;font-weight:bold">not <span style="color: #ff7700;font-weight:bold">in D: D<span style="color: black">[x<span style="color: black">] <span style="color: #66cc66">= <span style="color: #ff4500">1 <span style="color: #ff7700;font-weight:bold">else: D<span style="color: black">[x<span style="color: black">] +<span style="color: #66cc66">= <span style="color: #ff4500">1 <span style="color: #ff7700;font-weight:bold">return D <span style="color: #ff7700;font-weight:bold">def print_dict<span style="color: black">(h<span style="color: black">): <span style="color: #ff7700;font-weight:bold">for c <span style="color: #ff7700;font-weight:bold">in h: <span style="color: #ff7700;font-weight:bold">print<span style="color: black">(c<span style="color: #66cc66">, <span style="color: #483d8b">'=>'<span style="color: #66cc66">, h<span style="color: black">[c<span style="color: black">]<span style="color: black">) res_dictionary <span style="color: #66cc66">= count_instances<span style="color: black">(string1<span style="color: black">)print_dict<span style="color: black">(res_dictionary<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #008000"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 11: Copying Dictionary ElementsD2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}D_New <span style="color: #66cc66">= D2D2<span style="color: black">[<span style="color: #483d8b">'job'<span style="color: black">] <span style="color: #66cc66">= <span style="color: #483d8b">'data analyst'<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(D_New<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 12: Copying Dictionary Elements using copy()D2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}D_New <span style="color: #66cc66">= D2.<span style="color: #dc143c">copy<span style="color: black">(<span style="color: black">)D2<span style="color: black">[<span style="color: #483d8b">'job'<span style="color: black">] <span style="color: #66cc66">= <span style="color: #483d8b">'data analyst'<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated D2 dictionary:"<span style="color: #66cc66">, D2<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The copied dictionary D_New:"<span style="color: #66cc66">, D_New<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Joining Dictionaries
Merging Dictionaries Using update()
You can combine dictionaries using the update() method as shown below.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 13: Combining DictionariesD1 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}D2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original D1 dictionary:"<span style="color: #66cc66">, D1<span style="color: black">)D1.<span style="color: black">update<span style="color: black">(D2<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated D1 dictionary:"<span style="color: #66cc66">, D1<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 14: Combining Dictionaries: Secondary dict with same key as original dictD1 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}D2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data_analyst'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original D1 dictionary:"<span style="color: #66cc66">, D1<span style="color: black">)D1.<span style="color: black">update<span style="color: black">(D2<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated D1 dictionary:"<span style="color: #66cc66">, D1<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Merging Dictionaries using **
In the below example, we are creating a new dictionary which is resulting from merging of two dictionaries.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 15: Merging Dictionaries: using **D1 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}D2 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">28<span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original D1 dictionary:"<span style="color: #66cc66">, D1<span style="color: black">)D3 <span style="color: #66cc66">= <span style="color: black">{**D1<span style="color: #66cc66">,**D2<span style="color: black">}<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated D1 dictionary:"<span style="color: #66cc66">, D3<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 16: Creating List of Dictionarieslist_of_dictionaries <span style="color: #66cc66">= <span style="color: black">[<span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #66cc66">, <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data analyst'<span style="color: black">}<span style="color: black">] <span style="color: #808080;font-style: italic">#print the list<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(list_of_dictionaries<span style="color: black">) <span style="color: #808080;font-style: italic">#validating the elements<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #008000">type<span style="color: black">(list_of_dictionaries<span style="color: black">)<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #008000">type<span style="color: black">(list_of_dictionaries<span style="color: black">[<span style="color: #ff4500">0<span style="color: black">]<span style="color: black">)<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #008000"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #008000"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Appending a new dictionary to list of dictionaries
You can append a new dictionary to the list using the append() method as shown below.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 17: Appending a new dict to list of dictionaries using append()list_of_dict <span style="color: #66cc66">= <span style="color: black">[<span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #66cc66">, <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data analyst'<span style="color: black">}<span style="color: black">]<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The original list:"<span style="color: #66cc66">, list_of_dict<span style="color: black">)new_dict <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Dean'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'data scientist'<span style="color: black">} <span style="color: #808080;font-style: italic">#Appending the list of dictionaries with the new dictionary using append() methodlist_of_dict.<span style="color: black">append<span style="color: black">(new_dict<span style="color: black">) <span style="color: #808080;font-style: italic">#print the new list<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The updated list:"<span style="color: #66cc66">, list_of_dict<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 18: Accessing key-value pairslist_of_dict <span style="color: #66cc66">= <span style="color: black">[<span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #66cc66">, <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data analyst'<span style="color: black">}<span style="color: black">]<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The given list of dictionaries:"<span style="color: #66cc66">, list_of_dict<span style="color: black">) <span style="color: #808080;font-style: italic">#Accessing key value pairs using indexing - print the last element<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The key value pairs are:"<span style="color: #66cc66">, list_of_dict<span style="color: black">[-<span style="color: #ff4500">1<span style="color: black">]<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Updating the key-value pairs of list of dictionaries
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 19: Updating key-value pairslist_of_dict <span style="color: #66cc66">= <span style="color: black">[<span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #66cc66">, <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data analyst'<span style="color: black">}<span style="color: black">]<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The given list of dictionaries:"<span style="color: #66cc66">, list_of_dict<span style="color: black">) <span style="color: #808080;font-style: italic">#Adding a new key-value pair to the 1st dictionary of the listlist_of_dict<span style="color: black">[<span style="color: #ff4500">0<span style="color: black">]<span style="color: black">[<span style="color: #483d8b">'age'<span style="color: black">] <span style="color: #66cc66">= <span style="color: #ff4500">27 <span style="color: #808080;font-style: italic">#updating the existing key of 2nd dictionary of the listlist_of_dict<span style="color: black">[<span style="color: #ff4500">1<span style="color: black">]<span style="color: black">[<span style="color: #483d8b">'first_name'<span style="color: black">] <span style="color: #66cc66">= <span style="color: #483d8b">'dean' <span style="color: #808080;font-style: italic">#deleting the key pair of 1st dictionary<span style="color: #ff7700;font-weight:bold">del list_of_dict<span style="color: black">[<span style="color: #ff4500">0<span style="color: black">]<span style="color: black">[<span style="color: #483d8b">'job'<span style="color: black">] <span style="color: #808080;font-style: italic">#printing the updated list<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The key value pairs are:"<span style="color: #66cc66">, list_of_dict<span style="color: black">) </span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: #808080;font-style: italic"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
- next() function
- 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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 20: Searching a list of dictionarieslist_of_dict <span style="color: #66cc66">= <span style="color: black">[ <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #66cc66">, <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data analyst'<span style="color: black">} <span style="color: black">]<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(next<span style="color: black">(i <span style="color: #ff7700;font-weight:bold">for i <span style="color: #ff7700;font-weight:bold">in list_of_dict <span style="color: #ff7700;font-weight:bold">if i<span style="color: black">[<span style="color: #483d8b">"first_name"<span style="color: black">] <span style="color: #66cc66">== <span style="color: #483d8b">"Sammy"<span style="color: black">)<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(next<span style="color: black">(<span style="color: black">(i <span style="color: #ff7700;font-weight:bold">for i <span style="color: #ff7700;font-weight:bold">in list_of_dict <span style="color: #ff7700;font-weight:bold">if i<span style="color: black">[<span style="color: #483d8b">"job"<span style="color: black">] <span style="color: #66cc66">== <span style="color: #483d8b">"python"<span style="color: black">)<span style="color: #66cc66">, <span style="color: #483d8b">"Not Present"<span style="color: black">)<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 21: Searching a list of dictionaries: Using filter()list_of_dict <span style="color: #66cc66">= <span style="color: black">[ <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">,<span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: black">}<span style="color: #66cc66">, <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Sammy'<span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'data analyst'<span style="color: black">} <span style="color: black">]<span style="color: #008000">list<span style="color: black">(<span style="color: #008000">filter<span style="color: black">(<span style="color: #ff7700;font-weight:bold">lambda item: item<span style="color: black">[<span style="color: #483d8b">'first_name'<span style="color: black">] <span style="color: #66cc66">== <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, list_of_dict<span style="color: black">)<span style="color: black">) </span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #008000"></span style="color: black"></span style="color: #008000"></span style="color: black"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Example 22: Searching a list of dictionaries: Using filter()D1 <span style="color: #66cc66">= <span style="color: black">{<span style="color: #483d8b">'first_name' : <span style="color: #483d8b">'Jim'<span style="color: #66cc66">, <span style="color: #483d8b">'age' : <span style="color: #ff4500">23<span style="color: #66cc66">, <span style="color: #483d8b">'height' : <span style="color: #ff4500">6.0 <span style="color: #66cc66">, <span style="color: #483d8b">'job' : <span style="color: #483d8b">'developer'<span style="color: #66cc66">, <span style="color: #483d8b">'company': <span style="color: #483d8b">'XYZ'<span style="color: black">} <span style="color: #808080;font-style: italic">#fetch a value of dict by its key using get(key, default)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Is key named 'name' present?"<span style="color: #66cc66">, D1.<span style="color: black">get(<span style="color: #483d8b">'name'<span style="color: #66cc66">, <span style="color: #483d8b">'not present'<span style="color: black">))<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Is key named 'first_name' present?"<span style="color: #66cc66">, D1.<span style="color: black">get(<span style="color: #483d8b">'first_name'<span style="color: #66cc66">, <span style="color: #483d8b">'not present'<span style="color: black">)) <span style="color: #808080;font-style: italic">#remove an items based on key using pop(key, default)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Original Dict:"<span style="color: #66cc66">, D1<span style="color: black">)D1.<span style="color: black">pop(<span style="color: #483d8b">'company'<span style="color: black">)<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Updated Dict:"<span style="color: #66cc66">, D1<span style="color: black">) <span style="color: #808080;font-style: italic">#avoiding KeyError exceptions using setdefault(key, value)<span style="color: #808080;font-style: italic">#key is already present, so display associated valuex <span style="color: #66cc66">= D1.<span style="color: black">setdefault(<span style="color: #483d8b">"age"<span style="color: #66cc66">, <span style="color: #483d8b">"27"<span style="color: black">) <span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The value associated with key 'age':"<span style="color: #66cc66">, x<span style="color: black">)<span style="color: #808080;font-style: italic">#key is not present, so adds the value & displays ity <span style="color: #66cc66">= D1.<span style="color: black">setdefault(<span style="color: #483d8b">"company"<span style="color: #66cc66">, <span style="color: #483d8b">"xyz"<span style="color: black">) <span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"The value associated with key 'company':"<span style="color: #66cc66">, y<span style="color: black">)</span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #483d8b"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: #483d8b"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
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.
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