0% found this document useful (0 votes)
24 views2 pages

Dictionary Questions

Uploaded by

luckypandit626
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)
24 views2 pages

Dictionary Questions

Uploaded by

luckypandit626
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/ 2

WORKSHEET : DICTIONARY

1. What will be the output of the following statement?


a) keys = [1, 2, 4 ]
value='same'
d1=dict.fromkeys(keys,value)
print(d1)
b) car = { "brand": "Ford", "model": "Mustang", "year": 1964 }
x = car.setdefault("color", "White")
print(car.setdefault("brand"))
print(car.setdefault("price"))
print(x ,car) ->{1: 'same', 2: 'same', 4: 'same'}
2. While trying to work on a data collection Saurav makes a few
declarations:-
list1=[1,2,3]
list2=[‘red’ , ‘blue’ , green’ ]
dic1={}
s1=’covid-go-away’
s2=’The deadly virus’
Help Saurav to write the statements to do the following :-
a) Define keys of the dictionary as enum1 , enum2 b) Values for
enum1 and enum2 are list1 and list2. c)Display the dictionary
items.
d) Using s1 and s2 print the combined output as enum3(key for
dictionary)

3. What is the output of the following code?


d={}
d[1]=5
d[1]=4
d[1.0]=14
add=0
for i in d:
add=add+d[i]
print(add)
4. The output of the following Python code is raising an error?
dic1 = {0: ‘One’, 1: ‘Two’, 2: ‘Three’}
for x, y in dic1: ->dic1.items()
print(x, y)
TypeError: cannot unpack non-iterable int object .Make correction in the code to
get the output as
0 One
1 Two
2 Three

5. What will be the output of the following code:-


rec={'Name': 'python', 'Age': 20}
r=rec.copy()
print(id(r )==id(rec))

6. Given the dictionary x={'K1':'V1' ,'K2' :'V2' ,'K3' : 'V3'} , create a dictionary
with the opposite mapping i.e., write a program to create the dictionary
as : inverted_x (x1= {'V1': 'K1', 'V2': 'K2', 'V3': 'K3'})
x1={}
for a in x:
b=x[a]
x1[b]=a
print(x1)

You might also like