PYTHON

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Python Dictionary

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’.

A Dictionary in Python works similar to the Dictionary in a real world. Keys of a Dictionary must be unique and of immutable data type such as Strings, Integers and tuples, but the key-values can be repeated and be of any type.

Note – Keys in a dictionary doesn’t allows Polymorphism.

Creating a Dictionary

In Python, a Dictionary can be created by placing sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds a pair of values, one being the Key and the other corresponding pair element being its Key:value. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable.

Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}.

Note – Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly.

# Creating an empty Dictionary 
Dict = {} 
print("Empty Dictionary: ") 
print(Dict) 
  
# Creating a Dictionary  
# with Integer Keys 
Dict = {1: 'hai', 2: 'hello', 3: 'hai'} 
print("\nDictionary with the use of Integer Keys: ") 
print(Dict) 
  
# Creating a Dictionary  
# with Mixed keys 
Dict = {'Name': 'hai', 1: [1, 2, 3, 4]} 
print("\nDictionary with the use of Mixed Keys: ") 
print(Dict) 
  
# Creating a Dictionary 
# with dict() method 
Dict = dict({1: 'HAI', 2: 'HELLO', 3:'HAI'}) 
print("\nDictionary with the use of dict(): ") 
print(Dict) 
  
# Creating a Dictionary 
# with each item as a Pair 
Dict = dict([(1, 'hai'), (2, 'For')]) 
print("\nDictionary with each item as a pair: ") 
print(Dict) 
  
# Creating a Nested Dictionary 
Dict = {1: {'A' : 'hai', 'B' : 'For', 'C' : 'hai'}, 
        2: {'D' : 'Welcome', 'E' : 'To', 'F' : 'hai'}} 
print("\nNested Dictionary: ") 
print(Dict) 

Output:

Empty Dictionary: 
{}

Dictionary with the use of Integer Keys: 
{1: 'hai', 2: 'For', 3: 'hai'}

Dictionary with the use of Mixed Keys: 
{1: [1, 2, 3, 4], 'Name': 'hai'}

Dictionary with the use of dict(): 
{1: 'hai', 2: 'hello', 3: 'hai'}

Dictionary with each item as a pair: 
{1: 'hai', 2: 'hello'}

Nested Dictionary: 
{1: {'B': 'For', 'C': 'hai', 'A': 
'hai'}, 2: {'F': 'hai', 'E':
 'To', 'D': 'Welcome'}}

Adding elements to a Dictionary

In Python Dictionary, 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.

# Creating an empty Dictionary 
Dict = {} 
print("Empty Dictionary: ") 
print(Dict) 
  
# Adding elements one at a time 
Dict[0] = 'hai'
Dict[2] = 'hello'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ") 
print(Dict) 
  
# Adding set of values  
# to a single Key 
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ") 
print(Dict) 
  
# Updating existing Key's Value 
Dict[2] = 'Welcome'
print("\nUpdated key value: ") 
print(Dict) 
  
# Adding Nested Key value to Dictionary 
Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'hai'}} 
print("\nAdding a Nested Key: ") 
print(Dict) 

Output:

Empty Dictionary: 
{}

Dictionary after adding 3 elements: 
{0: 'hai', 2: 'hello', 3: 1}

Dictionary after adding 3 elements: 
{0: 'hai', 2: 'hello', 3: 1, 'Value_set': (2, 3, 4)}

Updated key value: 
{0: 'hai', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)}

Adding a Nested Key: 
{0: 'hai', 2: 'Welcome', 3: 1, 5: {'Nested': {'1': 
'Life', '2': 'hai'}}, 'Value_set': (2, 3, 4)}

Removing Elements from Dictionary

In Python Dictionary, deletion of keys can be done by using the del keyword. Using del keyword, specific values from a dictionary as well as whole dictionary can be deleted. Other functions like pop() and popitem() can also be used for deleting specific values and arbitrary values from a Dictionary. All the items from a dictionary can be deleted at once by using clear() method. Items in a Nested dictionary can also be deleted by using del keyword and providing specific nested key and particular key to be deleted from that nested Dictionary.

Note- del Dict will delete the entire dictionary and hence printing it after deletion will raise an Error.

# Initial Dictionary 
Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'hai', 
        'A' : {1 : 'hai', 2 : 'For', 3 : 'hai'}, 
        'B' : {1 : 'hai', 2 : 'hai'}} 
print("Initial Dictionary: ") 
print(Dict) 
  
# Deleting a Key value 
del Dict[6] 
print("\nDeleting a specific key: ") 
print(Dict) 
  
# Deleting a Key from 
# Nested Dictionary 
del Dict['A'][2] 
print("\nDeleting a key from Nested Dictionary: ") 
print(Dict) 
  
# Deleting a Key  
# using pop() 
Dict.pop(5) 
print("\nPopping specific element: ") 
print(Dict) 
  
# Deleting a Key 
# using popitem() 
Dict.popitem() 
print("\nPops first element: ") 
print(Dict) 
  
# Deleting entire Dictionary 
Dict.clear() 
print("\nDeleting Entire Dictionary: ") 
print(Dict) 

Output:

Initial Dictionary: 
{'A': {1: 'hai', 2: 'For', 3: 'hai'}, 'B': {1: 'hai', 2: 'Life'}, 5: 'Welcome', 6: 'To', 7: 'hai'}

Deleting a specific key: 
{'A': {1: 'hai', 2: 'For', 3: 'hai'}, 'B': {1: 'hai', 2: 'Life'}, 5: 'Welcome', 7: 'hai'}

Deleting a key from Nested Dictionary: 
{'A': {1: 'hai', 3: 'hai'}, 'B': {1: 'hai', 2: 'Life'}, 5: 'Welcome', 7: 'hai'}

Popping specific element: 
{'A': {1: 'hai', 3: 'hai'}, 'B': {1: 'hai', 2: 'Life'}, 7: 'hai'}

Pops first element: 
{'B': {1: 'hai', 2: 'Life'}, 7: 'hai'}

Deleting Entire Dictionary: 
{}

Dictionary Methods

METHODS DESCRIPTION
copy() They copy() method returns a shallow copy of the dictionary.
clear() The clear() method removes all items from the dictionary.
get() It is a conventional method to access a value for a key.
dictionary_name.values() returns a list of all the values available in a given dictionary.
str() Produces a printable string representation of a dictionary.
update() Adds dictionary dict2’s key-values pairs to dict
setdefault() Set dict[key]=default if key is not already in dict
keys() Returns list of dictionary dict’s keys
items() Returns a list of dict’s (key, value) tuple pairs
has_key() Returns true if key in dictionary dict, false otherwise
fromkeys() Create a new dictionary with keys from seq and values set to value.
type() Returns the type of the passed variable.
Resets the time conversion rules used by the library routines. The environment variable TZ specifies how this is done.
cmp() Compares elements of both dict.