Working with Dictionaries in Python - Contd.

Dictionaries

In the last post, I have explained how to create dictionary, add new values to the dictionary and remove the values from dictionary. 

We have also seen the below methods. 
  • setdefault() - Retrieve the data associated with the specific key passed. If key isn't present in the dictionary, new entry would be added. 
  • pop() - Returns the data associated with the key (passed) and deletes the entry from dictionary.
  • popitem() - Returns the last key and corresponding value from the dictionary. Accepts no parameters. 
Click Here to go through about these in detail.

We will see about some more methods associated with dictionaries in this post. 
  • keys() - Retrieve the keys present in the dictionary.
  • get() - Retrieve the value associated with the key passed. 
  • clear() - Clears the dictionary.
  • copy() - Copy the dictionary to other.
  • items() - Returns the items (key & value) present in the dictionary.
  • update() - Updates the dictionary with the elements from the other dictionary. 
  • values() - Returns the values present in the directory.
  • fromkeys() - Creates new dictionary from the values provided.
Let's have a look at these methods in detail by it's usage with an example.

Retrieve data from Dictionary

Depending on what data (Keys, Values or Both) we need to retrieve from the dictionary, we can use the below methods. 

If we need to retrieve the keys present in the dictionary, keys() method can be used to retrieve the keys.

Retrieve keys from a dictionary

keys() method can be used either to print all the keys at once (or assign it to a variable) or can be looped through in the for loop for each key value. 
  • In case of keys() in the print statement, all the keys would be printed once as type dict_keys.
  • In case of for loop, each key would be printed separately. 
Print the keys of a dictionary

If we need to retrieve the values from a dictionary, values() method can be used to retrieve the values. We will use the similar example as above to understand this easily. 

Retrieve values from a dictionary

values() method can be used either to print all the values at once (or assign it to a variable) or can be looped through in the for loop for each value. 
  • In case of values() in the print statement, all the values would be printed once as type dict_values.
  • In case of for loop, each value would be printed separately. 
Print values from a dictionary

Methods keys() and values() are helpful if we need to retrieve either keys or values separately. If we need to retrieve both the keys and values, items() method can be used. This method would return each key and value combination as a tuple. 

Retrieve data from dictionary

items() method can be used either to print all the items (key & value combination) at once (or assign it to a variable) or can be looped through in the for loop for each value. 
  • In case of items() in the print statement, all the values would be printed once as type dict_items.
  • In case of for loop, each key and value combination would be printed separately as a tuple.
Retrieve data from dictionary

Methods (keys(), values() and items()) explained above are helpful if we need to retrieve all the data present in the dictionary. We can use the method get(). This method accept key as an argument and returns the value. 

Retrieve data from dictionary for a specific key

This would print the value associated with the key (2) passed (i.e., "TWO"). 

Copy data from one dictionary to the other

There are couple of ways for copying data from one dictionary to the other. 
  • Copy (or append) the data from one dictionary to the other dictionary already created. 
  • Create the dictionary by copying the data from a dictionary. 
update() method is useful to copy the data from one dictionary to the other dictionary which is already created. This method accepts a dictionary as an argument and appends the data from this dictionary to the original dictionary. 

It can be easily understood with the below example. 

Copy data from one dictionary to the other

In the above example, data form dictionary 'b_dict' is appended to the dictionary 'a_dict'. Data in the dictionary 'b_dict' stay as is. 

Copy data from one dictionary to the other

If there is no dictionary is created already, New dictionary can be created by using copy() method and copy the data to the dictionary created. 

Copy data and create dictionary

Dictionary 'b_dict' would be created with the data from 'a_dict' and both dictionaries should hold same data at this point. Any new data added to 'a_dict' after this would not be reflected in 'b_dict'.

Creating a dictionary based on the keys from a List or Tuple

In the previous example, we have seen how to create a dictionary by copying the data from the other dictionary. 

Let's say we have a list of keys present in either List or Tuple and the value against these keys are same, fromkeys() method is useful in this scenario. 

E.g.: 

We have a list containing integers [1, 2, 3] and a tuple containing strings ("ONE", "TWO", "THREE") and we need to create the dictionary with a value 'int' for integers and 'str' for strings, instead of writing each key to the dictionary list or tuple can be passed. 

Create dictionary from the keys passed

Two dictionaries would be created with the same value for all the keys. 

Print dictionary in Python

Clear data from the dictionary

We can use the clear() method to clear the data from a dictionary. 

Clear data from the dictionary

Hope above details were a bit of help to understand more about Dictionaries. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Comments

Popular posts from this blog

What is the importance of User Open (USROPN) in RPGLE - IBM i

What is Artificial Intelligence?

What is Deep Learning? Beyond the Basics of AI