Dictionary Manipulation in Python

Sandhya Krishnan
3 min readMar 29, 2023
Photo by Pixabay: https://www.pexels.com/photo/black-and-white-book-business-close-up-267669/

A dictionary is a collection of key-value pairs, where each key is unique and mapped to a corresponding value. Dictionaries are denoted by curly braces { } and each key-value pair is separated by a colon.

An empty dictionary can be created as below, dictionary belongs to
<class ‘dict’>.

fruit_dict = {}
print("fruit_dict is:", fruit_dict)
print("type of fruit_dict is : ", type(fruit_dict))
fruit_dict is: {}
type of fruit_dict is : <class 'dict'>

Accessing Dictionary (key, value)

The keys in a dictionary must be unique and immutable (e.g. strings, numbers, and tuples) while the values can be of any data type. While the list element is accessed using indices, the dictionary elements are accessed using the keys, not the indices. All the keys of the dictionary can be obtained using keys()

import random

fruit_list = ['Apple', 'Mangoes', 'Banana', 'Strawberry', 'Pineapple']
print(f"First element of fruit_list - {fruit_list} is :", fruit_list[0])
fruit_dict = {}

for element in fruit_list:
fruit_dict[element] = random.randint(100, 200)
print(f"Value of key Apple in the {fruit_dict} is :", fruit_dict['Apple'])
print("All Keys of fruit_dicts are : ", fruit_dict.keys())
First element of fruit_list - ['Apple', 'Mangoes', 'Banana', 'Strawberry', 'Pineapple']  is : Apple
Value of key Apple in the {'Apple': 105, 'Mangoes': 142, 'Banana': 131, 'Strawberry': 184, 'Pineapple': 121} is : 105
All Keys of fruit_dicts are : dict_keys(['Apple', 'Mangoes', 'Banana', 'Strawberry', 'Pineapple'])

Adding new (key, value) pair

A new key-value pair can be added to the dictionary by assigning a new key with a value. For example, if we want to add Grapes to fruit_dict, it can be done as below :

fruit_dict['Grapes'] = 99
print("New fruit_dict is:", fruit_dict)
New fruit_dict is: {'Apple': 105, 'Mangoes': 142, 'Banana': 131, 'Strawberry': 184, 'Pineapple': 121, 'Grapes': 99}

Updating Existing Value for a key

For updating the existing value, we just new to assign the new value to the key, like when we add the new key-value pair.

fruit_dict['Mangoes'] = 200
print("New fruit_dict is:", fruit_dict)
New fruit_dict is: {'Apple': 105, 'Mangoes': 200, 'Banana': 131, 'Strawberry': 184, 'Pineapple': 121, 'Grapes': 99}

Deleting Key-Value Pair

A key-value pair can be removed from the dictionary, either using the delkeyword or by pop(). If we want to remove grapes, then it can be done as:

#By Using del keyword
del(fruit_dict['Grapes'])
print("New fruit_dict is:", fruit_dict)
New fruit_dict is: {'Apple': 105, 'Mangoes': 200, 'Banana': 131, 'Strawberry': 184, 'Pineapple': 121}
#By Using pop()
fruit_dict.pop("Banana")
print("New fruit_dict is:", fruit_dict)
New fruit_dict is: {'Apple': 105, 'Mangoes': 200, 'Strawberry': 184, 'Pineapple': 121}

Sorting

In a dictionary key or value can be sorted ascending or descending using sorted().

Sorting Keys in Ascending:

The dictionary can be sorted by its keys, you can use the sorted() function with the key argument set to lambda x: x[0], and values can be sorted with lambda x: x[1]

fruit_dict = {'Apple': 105, 'Mangoes': 142, 'Banana': 131, 'Strawberry': 184, 'Pineapple': 121, 'Grapes': 99}
sorted_key_dict = dict(sorted(fruit_dict.items(), key=lambda x: x[0]))
print("sorted_key_dict is : ", sorted_key_dict)
sorted_key_dict is :  {'Apple': 105, 'Banana': 131, 'Grapes': 99, 'Mangoes': 142, 'Pineapple': 121, 'Strawberry': 184}

Sorting Values in Ascending:

sorted_value_dict = dict(sorted(fruit_dict.items(), key=lambda x: x[1]))
print("sorted_value_dict is : ", sorted_value_dict)
sorted_value_dict is :  {'Grapes': 99, 'Apple': 105, 'Pineapple': 121, 'Banana': 131, 'Mangoes': 142, 'Strawberry': 184}

Sorting in Descending:

By setting reverse as True, the keys or values can be sorted in descending order.

sorted_key_dict = dict(sorted(fruit_dict.items(), key=lambda x: x[0], reverse=True))
print("sorted_key_dict in descending order is : ", sorted_key_dict)
sorted_value_dict = dict(sorted(fruit_dict.items(), key=lambda x: x[1], reverse=True))
print("sorted_value_dict in descending order is : ", sorted_value_dict)
sorted_key_dict in descending order is :  {'Strawberry': 184, 'Pineapple': 121, 'Mangoes': 142, 'Grapes': 99, 'Banana': 131, 'Apple': 105}
sorted_value_dict in descending order is : {'Strawberry': 184, 'Mangoes': 142, 'Banana': 131, 'Pineapple': 121, 'Apple': 105, 'Grapes': 99}

List Manipulation in Python

List Comprehension and Dictionary Comprehension

--

--