New Batch Starts on 16th Oct.
Module: Network Fundamentals (L2 L3 Protocol Testing)
Timing: 07:00 AM to 08:00 AM IST (Weekdays)
Sign Up Now !

Chapter 9: Python Dictionaries – Operations and Methods

Working with Dictionaries in Python

In Chapter 9, we’ll explore Python dictionaries, a data structure where each value is indexed using a key. Unlike lists, which use numerical indices, dictionaries allow you to use descriptive keys, making them more flexible for storing and accessing data. This concept is similar to what other programming languages call associative arrays or hash maps. In this chapter, we’ll go over different ways to create dictionaries, how to add and remove key-value pairs, and useful dictionary methods like get(), keys(), and values(). We’ll also look at how to create two-dimensional dictionaries using lists, nested dictionaries, and the defaultdict class from the collections module.

Different Ways of Creating a Dictionary

Explanation:

  • There are multiple ways to create dictionaries in Python. You can use curly braces {} with key-value pairs, the dict() constructor, or pass a list of tuples to dict().

Adding and Removing Key-Value Pairs

Explanation:

  • You can easily add new key-value pairs to a dictionary by assigning a value to a new key. The del keyword is used to remove key-value pairs from the dictionary.

Checking for Key Presence

Explanation:

  • The in and not in operators are used to check whether a specific key is present in the dictionary.

Dictionary Methods

Explanation:

  • The get() method returns the value for the given key, or a default value (0 in this case) if the key is not found.
  • keys() and values() return the keys and values of the dictionary, respectively.

Creating Two-Dimensional Dictionaries

Method 1: Using Lists as Values:

Explanation:

  • In this example, the dictionary stores lists as values, effectively creating a two-dimensional dictionary where rows are represented by the keys ('peter', 'jose') and columns by list indices.

Method 2: Using Dictionaries as Values:

Explanation:

  • In this method, dictionaries are used as values, providing a more descriptive structure. This allows for nested dictionaries where keys represent both rows and columns.

Method 3: Using defaultdict from collections:

Explanation:

  • defaultdict automatically initializes missing keys with a default value. In this case, the default value is an empty dictionary, allowing easy creation of nested dictionaries.

Programming Exercises / Interview Questions :

Following are the programs you need to practice to get a strong understanding of this topic or to prepare for interviews.

  1. Write a Python program to print all the keys and values from a given dictionary.
  2. Write a Python program to find and print the key(s) that correspond to a given value.
  3. Write a Python program to remove a key-value pair from the dictionary based on a given key.
  4. Write a Python program to find the length of a dictionary without using the len() function.
  5. Write a Python program to create a dictionary where keys are numbers from 1 to 5, and the values are squares of the keys.
  6. Write a Python program to update the value of a specific key in a dictionary.
  7. Write a Python program to create a dictionary from a list of tuples, where each tuple contains two elements: a key and a value.
  8. Write a Python program to invert a dictionary, swapping the keys and values. If multiple keys have the same value, store the keys in a list.
  9. Write a Python program to merge two dictionaries. If a key exists in both dictionaries, sum the values associated with that key.
  10. Write a Python program to count the frequency of each word in a given paragraph and store the result in a dictionary.
  11. Write a Python program to filter a dictionary and keep only the entries where the value is greater than a specified threshold.
  • Create a dictionary to store the information given above and :
    1. Write a Python program to print the value associated with a given key.
    2. Write a Python program to check if a given key is present in the dictionary. If the key is present, print its value; otherwise, add a new key-value pair to the dictionary.
  • Create a two-dimensional dictionary to store the information in the table given above and :
    1. Write a Python program to find and print the status of a given interface.
    2. Write a Python program to find and print the interface names and IP addresses of all interfaces that are “up.”
    3. Write a Python program to count and print how many Ethernet interfaces are in the database.
    4. Write a Python program to add a new entry to the above database.


    We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

    Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.