Friday, March 5, 2021

Dictionary Methods in Python for processing of constituent Key and Value Pair Data Objects

 

Dictionary Methods in Python


* There are various methods to process the elements of a dictionary

 * These methods are used for retrieving and manipulating the contents of a dictionary

 * These various methods have been summarised in the given description table


 ===================================

Methods to process Dictionaries in Python

===================================

 1) clear()

dict.clear()

This method removes all the key-value pairs from the dictionary 'd' .

 

2) copy()

d1 = dict.copy()

This method copies all the elements from the dictionary 'd' into a new dictionary object 'd1' .

 

3) fromkeys()

dict.fromkeys(s[,v])

This creates a new dictionary with keys from a given sequence 's' and all the values set to 'v'

 

4) get()

dict.get(k[,v])

This returns all the values associated with the key 'k' . If the key is not found , then it returns 'v'

 

5) items()

dict.items()

This returns an object that contains key-value pairs of dictionary item 'dict' . The key value pairs are stored as tuples in the objects .

 

6) keys()

dict.keys()

This returns a sequence of keys from the dictionary object "dict" .

 

7) values()

dict.values()

This returns a sequence of values from the dictionary object "dict"

 

8) update()

dict.update(x)

This method adds all elements from the dictionary 'x' to the dictionary item object 'dict'

 

9) pop()

dict.pop(k[,v])

This method removes the key 'k' and its associated value from the dictionary object "dict" and returns the value associated with that key "v" is returned . If the key is not found and "v" is not mentioned then "KeyError" exception error is raised .

 

10) setdefault()

dict.setdefault(k[,v])

If the key 'k' is found , then its value is returned and if the key is not found , then the k,v pair is stored into the dictionary 'd'. In the given program , we are going to retrieve the keys from a dictionary object using the keys() method . The keys() method returns dict_keys object that contains only keys . We will be also able to retrieve the values from the dictionary object using the values() method . This method "values()" returns all the values in the form of a dict_values object . Similarly , the items() method can be used to retrieve all the key-value pairs into the "dict_items"

method can be used to retrieve all the key-value pairs into the "dict_items" object .

 

==============================================

 

Program

A Python program to retrieve keys , values and key-value pairs from a dictionary object

# dictionary methods - create a dictionary with employee details

dict =

{

  'Name' : 'D001' ,

  'Id' : '001',

  'Salary' : 1000

}

 

# print the entire dictionary

print(dict)

# display only keys

print(" values in dict = ", dict.values())

# display both key and value pairs as tuples

print(" Items in Dictionary =", dict.items())

 

Output :

{

   'Name' : 'D001' ,

   'Id' : '001',

   'Salary' : 1000

}

Keys in dict =dict_keys(['Name','Id','Salary'])

Values in dict = dict_values(['D001','001','1000'])

Items in dict = dict_items([('Name','D001'),('Id',001),('Salary',1000)])

 

In the given program, we are creating a dictionary by entering the elements from the keyboard and when the user enters the elements from the keyboard inside the curly braces , then the values inside the dictionary object are treated as key value pairs of a dictionary by using the eval() function . Once the elements are entered , one can find the sum of the values using the sum() function over the values of the dictionary .


The screenshot for the used code implemented over Jupyter Notebook is given as below :



No comments:

Post a Comment