Chrome Extension

Python collection - Dictionaries in python

my_dict = {
    "first_name": "John",
    "last_name": "Deo",
    "occupation": "programmer"

print(my_dict["first_name"]) #print first_name value of dictionary

print(my_dict.get("first_name")) #same as above

my_dict["first_name"] = "Josef" #To change first_name value

my_dict.values() #return values of dictionary

my_dict["nick_name"] = "JDo" #To add key value to dictionary

my_dict.pop("last_name") #To remove a key value pair from dictionary based on key

del my_dict["last_name"] #Same as above

my_dict.clear() #empty a dictionary

my_dict.copy() #To copy a dictionary
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Dictionaries in python represent a key-value pair datastore in a variable. You can access the value of the dictionary item by its key name. You can also change and remove items from a dictionary using a key name. 

Was this helpful?

You can also create dictionaries in python using dict constructor. Here is the example

my_dict = dict("first_name" = "John", "last_name" = "Deo", "occupation" = "programmer")
Join Devsheet Ask a Question