Fatskills
Practice. Master. Repeat.
Study Guide: Python Programming: Dictionary Operations and Functions
Source: https://www.fatskills.com/python/chapter/python-programming-dictionary-operations-and-functions

Python Programming: Dictionary Operations and Functions

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~4 min read

We saw basics of Dictionary as Python data type. Here, we are going to discuss about dictionary operations and Python in-built methods and functions for dictionary.

Creating a Dictionary
A Python dictionary can be created by placing key value pairs separated by comma within the curly braces as shown in the below example.
E.g. biodata = {'Name': 'Julie', 'Age': 25, 'Height': '180cm’, ‘Profession’: ‘Banker’};
 

Accessing elements from Dictionary
Element values from dictionary can be procured by using square brackets along with the key in it as shown in the below example.
E.g. biodata ['Profession']; biodata [‘Age’];

Updating and Adding elements in Dictionary
In Python language, a new entry of key value pair can be done by adding a new key value pair with the name of the dictionary as shown in the below example.
E.g. biodata ['Company'] = "XYZ Ltd.";

An existing value in dictionary can be updated by using the assignment operator and assign a new value to the key element in the dictionary as shown in the below example.
E.g. biodata [‘Age’] = 22;

Deleting an element from Dictionary
In Python language, a complete key-value pair element can be deleted by using ‘del’ statement before square brackets along with the key in it which is to be deleted as shown in the below example.
E.g. del biodata [‘Company’];

Also, if we want to delete the entire dictionary, it can be done by using the ‘del’ statement before dictionary variable as shown in the below example.
E.g. del biodata;

In the below Python code example, firstly, we have created dictionaries (biodata and month). Secondly, we are accessing element values from dictionary. Thirdly, we are adding key value pair in dictionary and then updating the value of one of the key-value pair in dictionary. Lastly, we have deleted an entire key value pair from dictionary based on its key and the complete dictionary using ‘del’ statement.

Properties of Dictionary Keys
- Key cannot be duplicated. Therefore, only one entry per key is permitted in Python dictionary. If python interpreter encounters duplicate keys during assignment, the last assignment wins i.e. will be considered or overwrite the previous one.
- Keys in dictionary are immutable. It means we can use numbers, strings or tuples as dictionary keys but anything like ['key'] is not allowed in

Python dictionary data type.
Built-in Dictionary Functions & Methods

Below are Python built-in function for dictionary.

Function

Description

cmp(dict1, dict2)

This Python built-in function for dictionary compares elements of both dict.

len(dict)

This Python built-in function for dictionary gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

str(dict)

This Python built-in function for dictionary produces a printable string representation of a dictionary

type(variable)

This Python built-in function for dictionary returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.

 

Below are Python built-in methods for dictionary.

 

Methods

Description

dict.clear()

This Python built-in method for dictionary removes all elements of dictionary dict

dict.copy()

This Python built-in method for dictionary returns a shallow copy of dictionary dict

dict.fromkeys()

This Python built-in method for dictionary create a new dictionary with keys from seq and values set to value.

dict.get(key, default=None)

For key 'key', returns value or default if key not in dictionary

dict.has_key(key)

This Python built-in method for dictionary returns true if key in dictionary dict, false otherwise

dict.items()

This Python built-in method for dictionary returns a list of dict's (key, value) tuple pairs

dict.keys()

This Python built-in method for dictionary returns list of dictionary dict's keys

dict.setdefault(key, default=None)

This Python built-in method for dictionary is similar to get(), but will set dict[key]=default if key is not already in dict

dict.update(dict2)

This Python built-in method for dictionary adds dictionary dict2's key-values pairs to dict

dict.values()

This Python built-in method for dictionary returns list of dictionary dict's values