Py11: Dictionary
Dictionary is a data structure that stores information about an object in key and value pairs. Dictionary is also called hash or map in other programming languages.
Introduction
Dictionary is an ordered list of items. Instead of an index, it uses a key to refer to stored value. Dictionary is mutable, we can add new pair-value pairs and update existing values. Its entries are unique, so no two entries are the same.
Creating Dictionary
A dictionary can be created by using the curly brackets { and }.
dict_name = {} # empty dictionary
to create a dictionary with pairs of keys and values.
dict_name = {key1: value1, key2:value2,...., keyN:valueN}
The key can be an integer or a string, usually, it's a string. The value can be numbers, strings, lists, tuples, or dictionaries.
Example
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
print(a_dict)
Output
{'title': 'Peter Pan', 'year': 1911, 'author': 'J.M.Barrie'} >[Back to Top]
Display using Loop
Display using loop using the items() function. It returns the key and value of each item in the dictionary.
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
for item in a_dict.items(): # iterate dictionary items()
print(item) # print current item
Output
('title', 'Peter Pan') ('year', 1911) ('author', 'J.M.Barrie') >
To assign key and value obtained from items() function in a separate variable.
for key, value in a_dict.items():# iterate dictionary items.
print(key,"=", value)
Output
title = Peter Pan year = 1911 author = J.M.Barrie >
Other than this, the values() and the keys() function can be used to only get a list of keys or values. And, the len() function returns how many items are there in a dictionary.
[Back to Top]Accessing Dictionary
Instead of an index number, the key is used to refer to an element in the dictionary. The dict_name[key] code returns a value corresponding to the key.
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
print(a_dict['year']) # get value of 'year' key
Output
1911 >
And, dict_name.get(key) returns the value corresponding to the key, None if the key is not in the dictionary.
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
print(a_dict.get('year')) # get value of year
print(a_dict.get('pages')) # get value of pages
Output
1911 None >[Back to Top]
Modify Element
The following example shows how to modify a dictionary element.
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
a_dict['author'] = 'James Matthew Barrie' # update author key
print(a_dict) # print key:value in dictionary
Output
{'title': 'Peter Pan', 'year': 1911, 'author': 'James Matthew Barrie'} >[Back to Top]
Add Element
The following example shows how to add an element to a dictionary. An element with a key named "pages" with a value of 267 is added to the dictionary.
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
a_dict['pages'] = 267 # add a new element the key:value pair
print(a_dict) # print key:value in dictionary
Output
{'title': 'Peter Pan', 'year': 1911, 'author': 'J.M.Barrie', 'pages': 267} >[Back to Top]
Remove element
The following example shows how to delete an element from a dictionary. Element with a key value of "year" is removed from the dictionary.
a_dict = {'title':'Peter Pan', 'year':1911, 'author':'J.M.Barrie'}
del a_dict['year'] # delete the 'year' key: value pair
print(a_dict) # print key:value in dictionary
Output
{'title': 'Peter Pan', 'author': 'J.M.Barrie'} >[Back to Top]
A post by Cuber
Comments
Post a Comment