Dictionaries in Python

What is Dictionary in python?
  • Dictionary is an unordered collection of data in the form of key-value pair.
  • First element in the key-value pair is the key and the second element is the value.
  • Like list, dictionaries are mutable.
Creating a dictionary
  • Dictionaries are written inside curly brackets ({}) with comma-separated key-value pairs.
  • A colon (:) is used to separate key from its associated value.
  • An empty dictionary can be created by placing curly brackets {}.
  • We can also create dictionary by using built-in function dict() with argument of key-value pair sequence. Below example gives better understanding.
Creating Dictionary in Python
Accessing a dictionary
  • In list, the elements are accessed by their index position but this is not applicable in dictionary.
  • In Dictionary, elements (i.e, values) are accessed by their associated keys.
  • This can be done by passing its corresponding key in square brackets [].
Accessing Value by passing Key 

  • The above can also be done by using a method called get(). Both results will be same.
Accessing Value by get() method
  • If the referred key is not present in the dictionary, then it will throw an error. 
Referred Key not present in Dictionary
  • But in get() method, if the key is not present in the dictionary then it will return none instead of throwing error.
Referred Key not present - get() method
Iterating through a dictionary
  • As we know that the dictionary is in key-value pair. If we use normal for loop to iterate, then it will return only the keys present inside the dictionary.
Iterating using for loop
Output - Returns the keys present inside the dictionary
  • If our requirement is to take only the element (values) associated with keys in dictionary, then we can use method values() to return the values of the dictionary.
Values() method to return values of the dictionary
  • We also loop through both keys and values of dictionary by using method items(). This items() function gives both key and value, which can be used in for loop.
items() method to return key-value pairs
  • We can check for a particular key in a dictionary using 'in' keyword.
Checking particular key using 'in' keyword
Adding and Changing dictionary elements
  • As we know that the dictionaries are mutable, we can change existing items or add new items to the dictionary by normal assignment operation.
  • If the specified key already exist, then the existing value gets updated. If not, then it will add new key-value pair to the dictionary.
Adding and Changing dictionary elements
Removing elements from the dictionary
1) pop()
  • This method removes a particular item from the dictionary with provided key and returns the value.
pop() method - Removing Elements
Output - pop()
2) popitem()
  • This method removes a random item from the dictionary and returns the item pair (key-value pair) from the dictionary.
popitem()  - Removing Elements
Output - popitem()
3) clear()
  • This method clears/removes all the items from the dictionary.
Clear() - Removing Elements from Dictionary
Output - clear()
4) Del
  • Del keyword used to remove a particular item or the entire dictionary. 
Del - Removing elements from dictionary
Output - Del
  • print function throws error because "Phone" dictionary no longer exists.
Merging Dictionary - update()
  • This method updates the dictionary with the elements from another dictionary or from an iteratable key-value pair.
  • If the key from another dictionary is already present in the main dictionary, then it will update the item with new value in main dictionary. 
  • If not present in main, dictionary, then it will add new items (key-value pair) in main dictionary.
  • If no parameters are passed to the update(), then the dictionary remains unchanged.
Merging two dictionaries using update()
Copying a dictionary - copy()
  • Sometimes we may have requirement to take copy of the source dictionary, people feel like best solution is to assign dictionary to some other variable, like dict2 = dict1.
  • This is not the proper way of taking copy. The reason is, dict2 will act as reference only and whatever the changes made in dict1 will also be reflected in dict2.
  • To avoid this, we can use copy() built-in method.
Copying a dictionary - copy()
Output - copy()
Nested Dictionary
  • A dictionary present inside another dictionary is called nested dictionary.
Nested Dictionary
Dictionary methods
  • In Python, we have set of built-in methods that you can use on dictionary
Removes all the elements from the dictionary
Returns a copy of the dictionary
Returns a dictionary with the specified keys and value
Returns the value of the specified key
Returns a list containing a tuple for each key value pair
Returns a list containing the dictionary's keys
Removes the element with the specified key
Removes the last inserted key-value pair
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
Updates the dictionary with the specified key-value pairs
Returns a list of all the values in the dictionary

No comments:

Post a Comment