Python Dictionary

Dictionary
A dictionary in python is a collection of data which is unordered, indexed and changable. In python a dictionary can be created with the help of curly brackets an they have keys and values present in them.
Creating and printing a dictionary in python:

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

print(thisdict)


Accessing Items
The items in a dictionary can be accessed with the help of reffering to it's key name inside square brackets.
For e.g. if you want to find the value of "model" key then you need to write the following code:

x = thisdict["model"]


Change Values
If you want to change the value of any item then it is possible in dictionary, you just need to refer to it's key name.
For e.g. if you want to change the "year" to 2018: Change the "year" to 2018:

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict["year"] = 2018


Loop Through a Dictionary
You can loop through a dictionary by with the help of a for loop.
When you loop through a dictionary, then by defalut the keys are the return values of the dictionary, but there are some methods with the help of which you can return the values as well
For e.g. if you want to print all the key names in a dictionary one by one then you need to write the following code:

for x in thisdict:

     print(x)


If you want to Print all values in the dictionary, one by one then you need to write the following code:

for x in thisdict:

     print(thisdict[x])


Check if Key Exists
If you want to check that if a specific key is present in the dictionary or not then you can use the in keyword.

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

if "model" in thisdict:

     print("Yes, 'model' is one of the keys in the thisdict dictionary")


Dictionary Length
If you want to find out that how many items(i.e. key-value pairs) are present in a dictionary then you can use the len() method.

print(len(thisdict))


Adding Items
If you want to add new item to the dictionary then it can be done using a new index key and then assign a value to it:

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict["color"] = "red"

print(thisdict)


Removing Items
There are many methods with the help of which you can remove the items from a dictionary :
The pop() method removes the item with the specified key name:

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict.pop("model")

print(thisdict)


The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict.popitem()

print(thisdict)


The del keyword removes the item with the specified key name:

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

del thisdict["model"]

print(thisdict))


Copy a Dictionary
If you want to a dictionary then you cannot simply writedict2=dict1, because it will only generate a reference from dict2 to dict1 and if you make any changes in dict1 then it will automatically be shown in dict2.

Following are the ways with the help of which you can generate the copy of a dictionary:
The first one is the built-in methood copy() method, it will generate a copy the dictionary that is given as the parameter in the method.
For e.g. make a copy of a dictionary with the help of copy() method then you need to write the following code:

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

mydict = thisdict.copy()

print(mydict)

The second one is to use the built-in method dict() to generate the copy of a dictionary.

thisdict ={

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

mydict = dict(thisdict)

print(mydict)


The dict() Constructor
The dict() constructor is used to make a new dictionary:
It is used in the following way:

thisdict =dict(brand="Ford", model="Mustang", year=1964)

# note that keywords are not string literals

# note the use of equals rather than colon for the assignment

print(thisdict)