Dictionary
▪️ Dictionary is the inbuilt data type in python which organizes data in a collection of key-value pairs, where each key is unique and maps to a specific value.
▪️ Each element in the dictionary is a key–value pair and one individual key value pair is treated as an item in the dictionary.
▪️ Keys are immutable i.e. you can’t change the key once you created it and must be unique within a dictionary.
▪️ Values can be mutable i.e. you can change the value which is associated with a specific key.
▪️ The keys and values in the dictionary are case sensitive, you can use the same name as key with different cases.
Creation of dictionary:
You can create a dictionary in 2 ways.
👉 By using { } braces
👉 By using dict function
Using { } braces:
Syntax: variable_name = { }
You can create an empty dictionary just by assigning the curly braces to a variable where you want to store the dictionary.
Examples
1 2 3 4 5 6 7 |
:1. m={ } # empty dictionary my_dict = {'January': 1, 'February': 2, 'March': 3,'June': 6, 'July': 7, 'August': 8} print(my_dict) # prints all the items in the my_dict. OUTPUT: {'January': 1, 'February': 2, 'March': 3, 'June': 6, 'July': 7, 'August': 8} |
By using dict function.
Syntax: variable name = dict (items)
To create an empty dictionary with the dict ( ) function
1 2 3 4 5 6 7 8 9 10 |
Example 1: m= dict( ) # an empty dictionary To create a dictionary with items Example 2: INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) print(my_dict) # prints all the items. OUTPUT: {'January': 1, 'February': 2, 'March': 3, 'June': 6, 'July': 7, 'August': 8} |
Adding items to dictionary
Items can be added to the dictionaries in two different ways.
👉 Direct assignment
👉 Adding one dictionary to another dictionary using update method
Direct Assignment
You can add some items to the existing dictionary using the below syntax.
Syntax: dictionary_name[key]= value
Example:
1 2 3 4 5 6 7 8 |
INPUT: my_dict={'January': 1, 'February': 2, 'March': 3, 'June': 6} my_dict['July'] = 7 # adding one item my_dict['August'] = 8 # adding one item print(my_dict) OUTPUT: {'January': 1, 'February': 2, 'March': 3, 'June': 6, 'July': 7, 'August': 8} |
Adding one dictionary to another dictionary
First create one dictionary and add the newly created dictionary to the dictionary which is created first using update commands.
Example:
1 2 3 4 5 6 7 8 |
INPUT: my_dict={'January': 1, 'February': 2, 'March': 3, 'June': 6} new_dict={'July': 7, 'August': 8} # new items my_dict.update(new_dict) print(my_dict) # prints items that are updated. OUTPUT: {'January': 1, 'February': 2, 'March': 3, 'June': 6, 'July': 7, 'August': 8} |
Methods used in dictionary.
👉 clear ( )
Using this method, you can clear all the items in the dictionary i.e. using this method on any dictionary will result in an empty dictionary.
Example:
1 2 3 4 5 6 7 8 9 |
INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) print(my_dict) # print the above items in dict my_dict.clear() print(my_dict) # prints empty dictionary OUTPUT: {'January': 1, 'February': 2, 'March': 3, 'June': 6, 'July': 7, 'August': 8} { } |
👉 get ( )
If you want to get the value of the specific key from a dictionary, then you can use the get( ) method.
get( )method will take 2 parameters one is the key to the value that you want, and the other is the value that is to be returned if the mentioned key is not there in the dictionary.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) m=my_dict.get("June", "KEY_NOTFOUND") print(m) OUTPUT: 6 INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) m=my_dict.get("june", "KEY_NOTFOUND") print(m) OUTPUT: KEY_NOTFOUND |
👉 Items ( )
This method is used to get all the key value pairs in the dictionary in the form of a tuple with two elements each.
Example:
1 2 3 4 5 6 7 |
INPUT: my_dict = dict(Mango=1, Guava=2, Apple=3, Pine_Apple=3, Banana=4,Grapes=5) k=my_dict.items() print(k) # gives all the Items in the my_dict OUTPUT: dict_items([('Mango', 1), ('Guava', 2), ('Apple', 3), ('Pine_Apple', 3), ('Banana', 4), ('Grapes', 5)]) |
👉 keys ( )
This method returns a list of all the keys present in the specified dictionary.
Example:
1 2 3 4 5 6 7 |
INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) k=my_dict.keys() # variable to store all the keys print(k) # gives all the keys in the my_dict OUTPUT: dict_keys(['January', 'February', 'March', 'June', 'July', 'August']) |
👉 values ( )
This method returns a list of all the values present in the specified dictionary which includes duplicated values as well.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) k=my_dict.values() # variable to store all the items print(k) # gives all the values in the my_dict OUTPUT: dict_values([1, 2, 3, 6, 7, 8]) INPUT: my_dict = dict(Mango=1, Guava=2, Apple=3, Pine_Apple=3, Banana=4,Grapes=5) k=my_dict.values() print(k) # gives all the keys in the my_dict including duplicates OUTPUT: dict_values([1, 2, 3, 3, 4, 5]) |
👉 pop ( )
▪️ This method is used to remove and return the item value with the specified key from the dictionary.
▪️ pop ( ) will take two parameters by default one is the key that is to be removed from the dictionary and the other is the value that is to be returned if the mentioned key is not found in the dictionary.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) k=my_dict.pop("March", "NOT_FOUND") print(k) # print the value of the mentioned key if the key is not present in dictionary prints default value in this example it is "NOT_FOUND" OUTPUT:3 INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) k=my_dict.pop("December", "NOT_FOUND") print(k) # as the mentioned key is not there in dictionary it will prints “NOT_FOUND". OUTPUT: NOT_FOUND |
👉 popitem ( )
▪️ This method is used to remove and return the item i.e. key value pair which was added recently to the dictionary.
▪️ This method will not take any arguments and follows LIFO (last in first out ) principle.
▪️ The removed item will be returned in the form of tuple.
Example:
1 2 3 4 5 6 7 |
INPUT: my_dict = dict(January=1, February=2, March=3, June=6, July=7,August=8) k=my_dict.popitem( ) # variable to collect the popped item print(k) # prints the popped item. OUTPUT: ('August', 8) |
👉 copy ( )
This method is used to return a shallow copy of the specified dictionary.
Example:
1 2 3 4 5 6 7 |
INPUT: my_dict = dict(Mango=1, Guava=2, Apple=3, Pine_Apple=3, Banana=4,Grapes=5) k=my_dict.copy() # creating the copy of the print(k) OUTPUT: {'Mango': 1, 'Guava': 2, 'Apple': 3, 'Pine_Apple': 3, 'Banana': 4, 'Grapes': 5} |
Author : Venkat Vinod Kumar Siram
LinkedIn : https://www.linkedin.com/in/vinodsiram/
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Note: Please test scripts in Non Prod before trying in Production.