Piton

Python Dictionaries

Python Dictionaries

Python is an efficient and versatile programming language. It is one of the most frequently used high-level programming languages to perform data-related tasks due to its many supportive built-in modules and functions. To mention some of its many built-in data structures, it has arrays, lists, tuples, etc.

Dictionaries are one of the built-in data structures in Python. It holds the data in the form of a key-value pair. The keys are the unique value that acts as a representative of data. The key is also called as “an index value”. Data structures are a very important aspect of any programming language. They are used to store and manipulate the data in a well-organized and efficient way. Therefore, Python dictionaries are more useful when we need to store the data in a form of key-value pair and to access the data faster. The Python dictionaries return the data faster because the key value for every data is unique, therefore the searching time for data is reduced, and we get the result faster. This article explicates the Python dictionaries in detail.

Creating the Dictionary

The hash tables in Python are often implemented using the dictionaries. The hash tables store the data in the form of key-value pairs as the dictionaries do. You can create a dictionary by using the set of curly braces (). Each dictionary needs to have a name, like every variable does, and should have key values. Each key-value must be unique. The pairs of keys and values are separated or differentiated by putting a comma (,).  The key could be of any type, i.e., integer and string. A dictionary has the power to store an entire list as a value.

Let us create the dictionaries to store the various types of information.

#creating a blank dictionary
myDict =
#printing the dictionary
print(myDict)
#creating a dictionary to store student information
#the dictionary contains the list of courses
myDict = 'name':'Kamran Sattar Awaisi', 'age':25, 'email': '[email protected]',
'class': 'MSSE', 'smeseter':'4th','courses':['ASA','ASPM','ASQ']
print(myDict)
#creating a simple dictionary
myDict = 1:'orange', 2:'banana',3:'apple',4:'peach',5:'date'
#printing the dictionary
print(myDict)
#creating a customers dictionary for online shopping
myDict = 'item':'books','quantity':'3','price':'$200'
print(myDict)

Output

Accessing the Values from the Dictionary

The values from the dictionary are accessed by using the keys. Every key point to a specific value. While getting the value, the key is mentioned inside the square brackets ([]), and it returns the value.

If a dictionary contains a list, and we want to access a particular index value of the list, we can further define its index value in the square brackets. The index value in Python always starts from zero. The Python also provides a built-in get() function to access the values. Inside the get function, we write the name of the key, and it returns the value. First, let's see the use of square brackets to get the values, and later on, we will discuss the get() function as well.

#a program to access the values using []
#creating a dictionary to store student information
#the dictionary contains the list of courses
myDict = 'name':'Kamran Sattar Awaisi', 'age':25, 'email': '[email protected]',
'class': 'MSSE', 'smeseter':'4th','courses':['ASA','ASPM','ASQ']
print("Printing the values of student Dictionary")
#accessing the name value
print(myDict['name'])
#accessing the age value
print(myDict['age'])
#accessing the email value
print(myDict['email'])
#accessing the class value
print(myDict['class'])
#accessing the smeseter value
print(myDict['smeseter'])
#accessing the courses
print(myDict['courses'])
#accessing the first course value using the list index
print("The first course: ",myDict['courses'][0])
#accessing the second course value using the list index
print("The second course:", myDict['courses'][1])
#accessing the third course value using the list index
print("The third course: ",myDict['courses'][2])
#creating a simple dictionary
myDict = 1:'orange', 2:'banana',3:'apple',4:'peach',5:'date'
print("printing the values of fruits Dictionary")
#accessing values
print(myDict[1])
print(myDict[2])
print(myDict[3])
print(myDict[4])
print(myDict[5])
#creating a customers dictionary for online shopping
myDict = 'item':'books','quantity':'3','price':'$200'
print("printing the values of shopping site dictionary")
print(myDict['item'])
print(myDict['quantity'])
print(myDict['price'])

Output

Now, let us discuss the get() function to access the values. The get() function takes the key and return the value.

#a program to access the values using get() function
#creating a dictionary to store student information
#the dictionary contains the list of courses
myDict = 'name':'Kamran Sattar Awaisi', 'age':25, 'email': '[email protected]',
'class': 'MSSE', 'smeseter':'4th','courses':['ASA','ASPM','ASQ']
print("Printing the values of student Dictionary")
#prinitng name value
print(myDict.get('name'))
#prinitng age value
print(myDict.get('age'))
#prinitng email value
print(myDict.get('email'))
#prinitng class value
print(myDict.get('email'))
#prinitng smeseter value
print(myDict.get('smeseter'))
#printing courses value
print(myDict.get('courses')[0])
print(myDict.get('courses')[1])
print(myDict.get('courses')[2])

Updating the Dictionary

An existing dictionary can be updated by adding new values and changing the existing values. The new value can be added to the dictionary very easily by using the pair of square brackets.

Let us add the new values in the dictionaries and change some of the existing values.

#a program to update the dictionary
myDict = 'name':'Kamran Sattar Awaisi', 'age':25, 'email': '[email protected]',
'class': 'MSSE', 'smeseter':'4th','courses':['ASA','ASPM','ASQ']
#printing the existing dictionary
print(myDict)
#adding the address key-value pair
myDict['address'] = "CUI campus"
#changing the age value
myDict['age'] = '23'
#changing the smeseter value
myDict['smeseter'] = '3rd'
#printing the updated dictionary
print(myDict)

Output

Deleting or Removing the Items from the Dictionary

The items can be removed or deleted from the dictionary in various ways. To delete a particular item, we can use the del keyword and the pop() function. We can use the clear() function to delete all the items for the dictionary.

Let us see the example program for deleting or removing the items from the dictionary.

#a program to update the dictionary
myDict = 'name':'Kamran Sattar Awaisi', 'age':25, 'email': '[email protected]',
'class': 'MSSE', 'smeseter':'4th','courses':['ASA','ASPM','ASQ']
#printing the existing dictionary
print(myDict)
#using the del keyword to delete name
del myDict['name']
#using pop() function to delete the age
myDict.pop('age')
#printing the updated dictionary
print("The updated dictionary:")
print(myDict)
#deleting the complete dictionary elements
myDict.clear()
print(myDict)

Output

Determining the Length of Dictionary

The length of the dictionary can be determined by using the built-in len() function. The len() function returns the length of the dictionary, which is equal to the number of total key-value pairs.

#a program to determine the length of the dictionary
myDict = 'name':'Kamran Sattar Awaisi', 'age':25, 'email': '[email protected]',
'class': 'MSSE', 'smeseter':'4th','courses':['ASA','ASPM','ASQ']
#printing the length of the dictionary
print("The length of the dictionary is: ",len(myDict))

Output

Conclusion

The dictionaries are important data structures in Python to store data in form of key-value pairs. The data can be easily retrieved by using the key.

This article describes the use of Python dictionaries with the support of simple examples.

OpenTTD vs Simutrans
Creating your own transport simulation can be fun, relaxing and extremely enticing. That's why you need to make sure that you try out as many games as...
OpenTTD Tutorial
OpenTTD is one of the most popular business simulation games out there. In this game, you need to create a wonderful transportation business. However,...
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...