Dictionary – Python

Share via:

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

By using dict function.
Syntax: variable name = dict (items)
To create an empty dictionary with the dict ( ) function

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:

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:

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:

👉 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:

👉 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:

👉 keys ( )
This method returns a list of all the keys present in the specified dictionary.
Example:

👉 values ( )
This method returns a list of all the values present in the specified dictionary which includes duplicated values as well.
Examples:

👉 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:

👉 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:

👉 copy ( )
This method is used to return a shallow copy of the specified dictionary.
Example:

 

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
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment