Python Dictionary

Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.

  • Keys must be a single element
  • Value can be any type such as list, tuple, integer, etc.

In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.

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.

Example:

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.

Example:

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.

Example:

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.

Example:

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.

Example:

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.

Example:

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

Following 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.

Following are Python built-in methods for dictionary.

Methods Descriptions
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.

Enroll Yourself: Python Training

Copyright 1999- Ducat Creative, All rights reserved.