0% found this document useful (0 votes)
2 views

Program 7

The document describes a program that maps two lists into a dictionary in Python. It explains the structure and methods of dictionaries, including how to create, access, modify, and loop through them. The program includes a runtime test case demonstrating the mapping of user-inputted keys and values into a dictionary.

Uploaded by

newshunt535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 7

The document describes a program that maps two lists into a dictionary in Python. It explains the structure and methods of dictionaries, including how to create, access, modify, and loop through them. The program includes a runtime test case demonstrating the mapping of user-inputted keys and values into a dictionary.

Uploaded by

newshunt535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No.

TITLE : Program to map two lists into a dictionary.

Problem Description : The program takes two lists and maps two lists into a dictionary.

Theory / Analysis

Dictionary

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

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

Create and print a dictionary:

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

Print the "brand" value of the dictionary:

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

Get the value of the "model" key:

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

Get the value of the "model" key:

x = thisdict.get("model")

Get Keys

The keys() method will return a list of all the keys in the dictionary.

Example

Get a list of the keys:

x = thisdict.keys()

Change Values

You can change the value of a specific item by referring to its key name:
Example

Change the "year" to 2018:

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)

Loop Through a Dictionary

You can loop through a dictionary by using a for loop.

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

Print all key names in the dictionary, one by one:

for x in thisdict:
print(x)

Example

Print all values in the dictionary, one by one:

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

Make a copy of a dictionary with the copy() method:

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

clear() Removes all the elements from the dictionary

copy() Returns a copy of the dictionary

fromkeys() Returns a dictionary with the specified keys and value

get() Returns the value of the specified key


items() Returns a list containing a tuple for each key value pair

keys() Returns a list containing the dictionary's keys

pop() Removes the element with the specified key

popitem() Removes the last inserted key-value pair

setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with
the specified value

update() Updates the dictionary with the specified key-value pairs

values() Returns a list of all the values in the dictionary

Python dict() Function

Example

Create a dictionary containing personal information:

x = dict(name = "John", age = 36, country = "Norway")

Program to map two lists into a dictionary.

1. Declare two empty lists and initialize them to an empty list.

3. Consider a for loop to accept values for the two lists.

4. Take the number of elements in the list and store it in a variable.

5. Accept the values into the list using another for loop and insert into the list.

6. Repeat 4 and 5 for the values list also.

7. Zip the two lists and use dict() to convert it into a dictionary.

8. Print the dictionary.

9. Exit.

Program

keys=[]

values=[]
n=int(input("Enter number of elements for dictionary:"))

print("For keys:")

for x in range(0,n):

element=int(input("Enter element" + str(x+1) + ":"))

keys.append(element)

print("For values:")

for x in range(0,n):

element=int(input("Enter element" + str(x+1) + ":"))

values.append(element)

d=dict(zip(keys,values))

print("The dictionary is:")

print(d)

Runtime Test Cases

Case 1:

Enter number of elements for dictionary:3

For keys:

Enter element1:1

Enter element2:2

Enter element3:3

For values:

Enter element1:1

Enter element2:4

Enter element3:9

The dictionary is:

{1: 1, 2: 4, 3: 9}
Case 2:

Enter number of elements for dictionary:2

For keys:

Enter element1:23

Enter element2:46

For values:

Enter element1:69

Enter element2:138

The dictionary is:

{46: 138, 23: 69}

You might also like