Navbar menu

Dictionaries in Python

What is Dictionary in python?
  • Dictionary is an unordered collection of data in the form of key-value pair.
  • The 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 a dictionary by using the built-in function dict() with an argument of key-value pair sequence. The below example gives a better understanding.
Creating Dictionary in Python
Output
Accessing a dictionary
  • In list, the elements are accessed by their index position but this is not applicable in a 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 

Output
  • The above can also be done by using a method called get(). Both results will be the same.
Accessing Value by get() method
Output
  • If the referred key is not present in the dictionary, then it will throw an error. 
Referred Key not present in Dictionary
Output
  • But in get() method, if the key is not present in the dictionary then it will return none instead of throwing an error.
Referred Key not present - get() method
Output
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 the dictionary, then we can use method values() to return the values of the dictionary.
Values() method to return values of the dictionary
Output
  • We also loop through both keys and values of the 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
Output
  • We can check for a particular key in a dictionary using 'in' keyword.
Checking particular key using the 'in' keyword
Output
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 exists, then the existing value gets updated. If not, then it will add new key-value pair to the dictionary.
Adding and Changing dictionary elements
Output
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 the dictionary
Output - Del
  • The print function throws an error because the "Phone" dictionary no longer exists.
Merging Dictionary - update()
  • This method updates the dictionary with the elements from another dictionary or from an iterable key-value pair.
  • If the key from another dictionary is already present in the main dictionary, then it will update the item with a new value in the main dictionary. 
  • If not present in the main, dictionary, then it will add new items (key-value pair) in the main dictionary.
  • If no parameters are passed to the update(), then the dictionary remains unchanged.
Merging two dictionaries using update()
Output
Copying a dictionary - copy()
  • Sometimes we may have a requirement to take a copy of the source dictionary, people feel like the best solution is to assign a dictionary to some other variable, like dict2 = dict1.
  • This is not the proper way of taking copies. 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 the nested dictionary.
Nested Dictionary
Output
Dictionary methods
  • In Python, we have set of built-in methods that you can use on a dictionary
Method
Description
clear()
Removes all the elements from the dictionary
copy()
Returns a copy of the dictionary
fromkeys()
Returns a dictionary with the specified keys and value
get()
Returns the value of the specified key
items()
Returns a list containing a tuple for each key-value pair
keys()
Returns a list containing the dictionary's keys
pop()
Removes the element with the specified key
popitem()
Removes the last inserted key-value pair
setdefault()
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()
Updates the dictionary with the specified key-value pairs
values()
Returns a list of all the values in the dictionary


No comments:

Post a Comment