Home >>Python Tutorial >Python Dictionary

Python Dictionary


Learn Python Dictionary

We have mentioned this fact before in the tutorial that dictionaries are basically a collection which is indexed, unordered, and changeable. It is also important for you to remember that in Python dictionary methods are written with curly brackets and those dictionaries further have keys and values.

For example, if you have to create and print a dictionary then

thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

print (thisdict)

 

To Access Items

While using this programming language, if you wish to access any particular item that is present in the dictionary then you can do that by referring to the key name of that particular item.

For example, if you want to get the value of the “model” key then

x = thisdict [ “model” ]

You can also use the get () method as this method will give you the same results. For example

x = thisdict.get (“model”)

 

To Change Values

If you wish to change the value of any particular item that is present in the dictionary then you can do that by referring to the key name of that particular item. If you wish to change the year from “1964” to “2018” then the python dictionary example for this is mentioned below

thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

thisdict [“year”] = 2018

 

To Loop Through a Dictionary

If you wish to loop through the various items that are present in the dictionary then you can do that by using the for loop function. It is also important for you to remember that the keys of the dictionary are actually the return values. There are also many other methods that you can use to return the values too.

For example, if you wish to print all the key names in the dictionary then

for x in thisdict:

print (x)

If you wish to print all the values in the dictionary then to do that you can follow the below-mentioned procedure

for x in the dictionary:

print (thisdict [x] )

If you wish to return the values of the dictionary then you can use the values () function. The python dictionary example for this is mentioned below

for x in thisdict.values( ) :

    print (x)

If you wish to loop through both the values and the keys of the dictionary then you can do that by using the items () function. For example,

for x, y in thisdict.items ( ) :

   print (x, y)

 

To Determine the Dictionary Length

If you wish to determine the total number of items that are present in a dictionary then you can do that by using the len () method. For example, if you want to print the total number of items that are present in a dictionary then print ( len (thisdict) )  

To Add Items

If you wish to perform the function of python dictionary add items then you can do that by using the index key and assigning a particular value to it.

For example

thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

thisdict [“color”] = “red”

print (thisdict)

 

To Remove Items

If you wish to remove any particular item from the dictionary then there are several different methods that you can follow to accomplish that particular task.

And some of those methods are given below

  • The del keyword helps in removing the specified key item. For example
thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

del thisdict [“model”]

print (thisdict)

 

  • If you wish to remove an item with a specified key name then you can also use the pop () method. The python dictionary example for this is mentioned below
thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

thisdict.pop (“model”)

print (thisdict)

 

  • If you wish to remove the last item from the items mentioned in the dictionary then you can do that by using the popitem () method. For example
thisdict = {

“brand” : “Ford”,

“model” : “Mustang”

“year” : 1964

}

thiddict.popitem( )

print (thisdict)

 

  • You can also use the del keyword method to remove any of the specified items from the dictionary. The python dictionary example for this is mentioned below
thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : “1964”

}

del thisdict [“model”]

print (thisdict)

 

  • You can also use the del keyword method to delete the dictionary completely. For example
thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

del thisdict

print (thisdict) #this will cause an error because “thisdict” no longer exists.

 

  • If you want to empty the dictionary entirely then you can use the clear () keyword. The Python dictionary example for this is mentioned below
thisdict = {

“brand” : “Ford”,

“model” : “Mustang”,

“year” : 1964

}

thisdict.clear ( )

print (thisdict)

 

To Construct a Dictionary

As a developer who is using this programming language, if you wish to construct a dictionary then you can do that by using the dict () constructor.

The python dictionary example for this is mentioned below

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)

 

The Dictionary Methods

Python has a number of different built-in python dictionary methods that any individual can use in the dictionaries. Some of those methods are given below.

Methods Description
clear () Helps in removing all the items
copy () Helps in returning a copy of the entire dictionary
fromkeys () Helps in returning a dictionary that contains specific keys and values
get () Helps in returning the value of the key that has been specified
items () Helps in returning a list that has a tuple for every single value pair
keys () Returns the entire list that contains all the dictionary keys
pop () Helps in removing the specified key elements
popitem () Helps in removing the last key-value pair
setdefault () Helps in returning the value of the specified key
update () Helps in updating the dictionary with the specified keys
values () Helps in returning a list of all the specified values

With this, we finish our Python dictionary operations module of this Python tutorial.