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
We are a bunch of lazy programmers so we developed this chrome extension
search code from your favorite search engine
Install Now

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")
Open Code Manager We are hiring