Dictionary in Python
Designed and Lectured by :- Deepak Singh
Asst.Prof. CS/IT
MCMT MALDAHIA VARANASI
Dictionaries in Python
A Python dictionary is a data structure that stores the value in key:value pairs.
Dict = {1: ‘Hello', 2: ‘Welcome', 3: ‘Pyton‘,3:”Dictionary”}
print(Dict)
Output
{1: ‘Hello', 2: ‘Welcome', 3: ‘Pyton‘,3:”Dictionary”}
Python Dictionary Syntax
dict_var = {key1 : value1, key2 : value2, …..}
What is a Dictionary in Python?
Dictionaries in Python is a data structure, used to store values in key:value format.
This makes it different from lists, tuples, and arrays as in a dictionary each key has an
associated value.
Note: As of Python version 3.7, dictionaries are ordered and can not contain duplicate keys.
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Dictionary Example
A dictionary can also be created by the built-in function dict(). An empty dictionary can be
created by just placing curly braces{}.
Different Ways to Create a Python Dictionary
The code demonstrates different ways to create dictionaries in Python. It first creates an
empty dictionary, and then shows how to create dictionaries using the dict() constructor with
key-value pairs specified within curly braces and as a list of tuples.
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Output
Dict = dict({1: ‘Apple', 2: ‘Banana', 3: 'Grapes'})
Empty Dictionary:
print("\nDictionary with the use of dict(): ") {}
print(Dict)
Dictionary with the use of dict():
{1: 'Apple', 2: 'Banana', 3: 'Grapes'}
Dict = dict([(1, 'Germany'), (2, ‘japan')])
Dictionary with each item as a pair:
print("\nDictionary with each item as a pair: ") {1: 'Germany', 2: ‘Japan'}
print(Dict)
Adding Elements to a Dictionary
The addition of elements can be done in multiple ways. One value at a time can be added to a
Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’.
Updating an existing value in a Dictionary can be done by using the built-in update() method.
Nested key values can also be added to an existing Dictionary.
Note- While adding a value, if the key-value already exists, the value gets updated otherwise a
new Key with the value is added to the Dictionary.
Example: Add Items to a Python Dictionary with Different DataTypes
The code starts with an empty dictionary and then adds key-value pairs to it. It demonstrates
adding elements with various data types, updating a key’s value, and even nesting dictionaries
within the main dictionary. The code shows how to manipulate dictionaries in Python.
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)
Dict[5] = {'Nested': {'1': 'Life', '2': 'Geeks'}}
print("\nAdding a Nested Key: ")
print(Dict)
Dictionary Methods
Here is a list of in-built dictionary functions with their description. You can use these functions to
operate on a dictionary.
Method Description
dict.clear() Remove all the elements from the dictionary
dict.copy() Returns a copy of the dictionary
dict.get(key, default = “None”) Returns the value of specified key
dict.items() Returns a list containing a tuple for each key value pair
dict.keys() Returns a list containing dictionary’s keys
dict.update(dict2) Updates dictionary with specified key-value pairs
dict.values() Returns a list of all the values of dictionary
pop() Remove the element with specified key
popItem() Removes the last inserted key-value pair
set the key to the default value if the key is not specified in
dict.setdefault(key,default= “None”)
the dictionary
dict.has_key(key) returns true if the dictionary contains the specified key.