This Python dictionary exercise aims to help Python developers to learn and practice dictionary operations. All questions are tested on Python 3. Show
Python dictionary is a mutable object, and it contains the data in the form of key-value pairs. Each key is separated from its value by a colon ( 2).Dictionary is the most widely used data structure, and it is necessary to understand its methods and operations. Also Read:
This Python dictionary exercise includes the following: –
It covers questions on the following topics:
When you complete each question, you get more familiar with the Python dictionary. Let us know if you have any alternative solutions. It will help other developers.
Table of contentsExercise 1: Convert two lists into a dictionaryBelow are the two lists. Write a Python program to convert them into a dictionary in a way that item from list1 is the key and item from list2 is the value
Expected output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30} Show Hint Use the 3 function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.Or, Iterate the list using a for loop and range() function. In each iteration, add a new key-value pair to a dict using the 4 methodShow Solution Solution 1: The zip() function and a 5 constructor
Solution 2: Using a loop and 4 method of a dictionary
Exercise 2: Merge two Python dictionaries into one
Expected output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50} Show Solution Python 3.5+
Other Versions
Exercise 3: Print the value of key ‘history’ from the below dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30} dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}0
Expected output: 80 Show Hint It is a nested dict. Use the correct chaining of keys to locate the specified key-value pair. Show Solution
Exercise 4: Initialize dictionary with default valuesIn Python, we can initialize the keys with the same values. Given: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}0 Expected output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}1 Show Hint Use the 1 method of dict.Show Solution The 1 method returns a dictionary with the specified keys and the specified value.{'Ten': 10, 'Twenty': 20, 'Thirty': 30}2 Exercise 5: Create a dictionary by extracting the keys from a given dictionaryWrite a Python program to create a new dictionary by extracting the mentioned keys from the below dictionary. Given dictionary: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}3 Expected output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}4 Show Hint
Show Solution Solution 1: Dictionary Comprehension {'Ten': 10, 'Twenty': 20, 'Thirty': 30}5 Solution 2: Using the 4 method and loop{'Ten': 10, 'Twenty': 20, 'Thirty': 30}6 Exercise 6: Delete a list of keys from a dictionaryGiven: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}7 Expected output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}8 Show Hint
To achieve the above result, we can use the dictionary comprehension or the 4 method of a dictionary.Show Solution Solution 1: Using the 4 method and loop{'Ten': 10, 'Twenty': 20, 'Thirty': 30}9 Solution 2: Dictionary Comprehension 0Exercise 7: Check if a value exists in a dictionaryWe know how to check if the key exists in a dictionary. Sometimes it is required to check if the given value is present. How do I change the key name in a nested dictionary in Python?To change a single dictionary just reference the dictionary you want to change. And if you want to change yes entries across all the dicts you have to a loop over dict. values . Note that this won't maintain the order of the keys.
How do I change the keyUse the built-in update() method to merge the keys and values of one nested dictionary into another. Note that this method blindly overwrites values of the same key if there's a clash.
How do you rename a key in a dictionary in Python?Change dictionary key in Python. Add new item and then remove old one. With del statement. With pop() method.. Define a function to change the key of a dictionary. If the old key does not exist, add a new item. If the old key does not exist, do nothing.. Can you modify the keys in a dictionary Python?Can you modify the keys of a dictionary? Yes we can modify the content of a dictionary. only values part of dictionary is modifiable.
|