Python Dictionary

dictionary in python

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

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.

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

Example:

# Creating a Dictionary  
# with Integer Keys 
Dict = {1: 'Ask', 2: 'with', 3: ' AskAtul '} 
print("\nDictionary with the use of Integer Keys: ") 
print(Dict) 
  
# Creating a Dictionary  
# with Mixed keys 
Dict = {'Name': ' AskAtul ', 1: [1, 2, 3, 4]} 
print("\nDictionary with the use of Mixed Keys: ") 
print(Dict) 

Output:

Dictionary with the use of Integer Keys: 
{1: 'Ask', 2: 'with', 3: 'Atul'}

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

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.

Example:

# Creating an empty Dictionary 
Dict = {} 
print("Empty Dictionary: ") 
print(Dict) 
  
# Adding elements one at a time 
Dict[0] = 'Ask'
Dict[2] = 'Atul'
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' : ' AskAtul '}} 
print("\nAdding a Nested Key: ") 
print(Dict) 

Output:

Empty Dictionary: 
{}

Dictionary after adding 3 elements: 
{0: 'AskAtul', 2: 'Atul', 3: 1}

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

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

Adding a Nested Key: 
{0: 'Ask', 2: 'Welcome', 3: 1, 5: {'Nested': {'1': 'Life', '2': 'Ask'}}, 'V

Accessing elements from a Dictionary

In order to access the items of a dictionary refer to its key name.Key can be used inside square brackets.

Example:

# Python program to demonstrate   
# accessing a element from a Dictionary  
  
# Creating a Dictionary  
Dict = {1: 'Ask', 'name': 'for', 3: 'Atul'} 
  
# accessing a element using key 
print("Accessing a element using key:") 
print(Dict['name']) 
  
# accessing a element using key 
print("Accessing a element using key:") 
print(Dict[1]) 

Output:

Accessing a element using key:
for

Accessing a element using key:
Atul

Removing Elements from Dictionary

Using del keyword

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

Using pop() method

Pop() method is used to return and delete the value of the key specified.

Using popitem() method

The popitem() returns and removes an arbitrary element (key, value) pair from the dictionary.

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *