Program 7
Program 7
Problem Description : The program takes two lists and maps two lists into a dictionary.
Theory / Analysis
Dictionary
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are
unordered.
Dictionaries are written with curly brackets, and have keys and values:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(thisdict)
Dictionary Items
Dictionary items are ordered, changeable, and does not allow duplicates.
Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(thisdict["brand"])
Accessing Items
You can access the items of a dictionary by referring to its key name, inside square brackets:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = thisdict["model"]
There is also a method called get() that will give you the same result:
Example
x = thisdict.get("model")
Get Keys
The keys() method will return a list of all the keys in the dictionary.
Example
x = thisdict.keys()
Change Values
You can change the value of a specific item by referring to its key name:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
thisdict["year"] = 2018
Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
thisdict["color"] = "red"
print(thisdict)
When looping through a dictionary, the return value are the keys of the dictionary, but there are
methods to return the values as well.
Example
for x in thisdict:
print(x)
Example
for x in thisdict:
print(thisdict[x])
Copy a Dictionary
You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a
reference to dict1, and changes made in dict1 will automatically also be made in dict2.
There are ways to make a copy, one way is to use the built-in Dictionary method copy().
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
mydict = thisdict.copy()
print(mydict)
Dictionary Methods
Python has a set of built-in methods that you can use on dictionaries.
Method Description
setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with
the specified value
Example
5. Accept the values into the list using another for loop and insert into the list.
7. Zip the two lists and use dict() to convert it into a dictionary.
9. Exit.
Program
keys=[]
values=[]
n=int(input("Enter number of elements for dictionary:"))
print("For keys:")
for x in range(0,n):
keys.append(element)
print("For values:")
for x in range(0,n):
values.append(element)
d=dict(zip(keys,values))
print(d)
Case 1:
For keys:
Enter element1:1
Enter element2:2
Enter element3:3
For values:
Enter element1:1
Enter element2:4
Enter element3:9
{1: 1, 2: 4, 3: 9}
Case 2:
For keys:
Enter element1:23
Enter element2:46
For values:
Enter element1:69
Enter element2:138