0% found this document useful (0 votes)
12 views17 pages

Dictionaries Examples

Uploaded by

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

Dictionaries Examples

Uploaded by

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

Examples - Dictionaries

Q1.
sortFruitSet = ['T', 'M', 'D', 'L', 'A']
sortFruitSet.sort()
print("Sorted List = ", sortFruitSet)

Answer:
Sorted List = ['A', 'D', 'L', 'M', 'T']

sortFruitSet = ['T', 'M', 'D', 'L', 'A']


sortFruitSet.sort(reverse=True)
print("Sorted List = ", sortFruitSet)

Answer:
Sorted List = ['T', 'M', 'L', 'D', 'A']

# create a list of prime numbers


prime_numbers = [2, 3, 5, 7]

# reverse the order of list elements


prime_numbers.reverse()
print('Reversed List:', prime_numbers)
# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order


for o in reversed(systems):
print(o)

Q2.
myDict = {1: 'Audi', 2: 'BMW' , 3: 'Ford', 4: 'Kia'}
print("Dictionary Items: ", myDict)

Answer:
Dictionary Items: {1: 'Audi', 2: 'BMW', 3: 'Ford', 4:
'Kia'}

Q3.
var = { "class" : 'V', "section" : 'A', "roll_no" : 12 }
print(var["class"])
print(var["section"])
print(var["roll_no"])
print(var)
Answer:
V
A
12
{'class': 'V', 'section': 'A', 'roll_no': 12}

Q4.
D = dict(One = "One", Two = "Two", Three = "Three")
print(D)

Answer:
{'One': 'One', 'Two': 'Two', 'Three': 'Three'}

D = dict(One = "One", Two = "Two", Three = "Three")


print(D["Two"])

Answer:
Two

Q5.
D = { 1:"Complete Assignments", 2:"Research It",
3:"Finish Projects" }
print("Step 3 = ", D[3])
Answer:
Step 3 = Finish Projects

Q6.
D = {"Planet" : "Earth",
"Continent" : "Asia",
"Country" : "Uzbekistan"}

print(D.get("Planet"))
print(D.get("Continent"))
print(D.get("Uzbekistan"))

Answer:
Earth
Asia
None

Q7.

D = {"First key" :("value1", "value2", "value3"),


"Another Key" : 22}
print(D["First key"])

Answer:
('value1', 'value2', 'value3')
Q8.
D = {}
D["Planet"] = "Earth"
D["Continent"] = "Asia"
D["Country"] = "Uzbekistan"
print(D)

Answer:
{'Planet': 'Earth',
'Continent': 'Asia',
'Country': 'Uzbekistan'}

Q9.
D = {}
D["Letters"] = "a", "b" "c", "d" "e", "f"
print(D)

Answer:
{'Letters': ('a', 'bc', 'de', 'f')}

Q10.
D_numbers = {2:8, 4:3, 5:9, 0:8, 21:18, 44:748,
90:184 }
D_numbers.popitem()
print(D_numbers)
D_numbers.pop(0)
D_numbers.pop(44)
print(D_numbers)

Answer:
{2: 8, 4: 3, 5: 9, 0: 8, 21: 18, 44: 748}
{2: 8, 4: 3, 5: 9, 21: 18}

Q11.
D_numbers = {2:8, 4:3, 5:9, 0:8, 21:18, 44:748,
90:184 }
del D_numbers[21]
del D_numbers[2]
del D_numbers[4]
print(D_numbers)
D_numbers.clear()
print(D_numbers)

Answer:
{5: 9, 0: 8, 44: 748, 90: 184}
{}

Q12.
D_numbers = {1:"Two", 3:4, 5:6 }
print(D_numbers)
D_numbers[1] = 2
print(D_numbers)

Answer:
{1: 'Two', 3: 4, 5: 6}
{1: 2, 3: 4, 5: 6}

Q13.
D = {1:2, 3:4, 5:6 }
print(len(D))

Answer:
3

Q14.

D = {"OS": "Windows", "Company": "Microsoft" }


D_var = D.copy()
print((D_var))
Answer:
{'OS': 'Windows', 'Company': 'Microsoft'}

Q15.

food = {'Tom': 'Burger', 'Jim': 'Pizza', 'Tim': 'Donut'}


f = food.keys()
print(f)

Answer:
dict_keys(['Tom', 'Jim', 'Tim'])

Q16.
food = {'Tom': 'Burger', 'Jim': 'Pizza', 'Tim': 'Donut'}
f = food.values()
print(f)

Answer:
dict_values(['Burger', 'Pizza', 'Donut'])

Q17.

employee = {1020: 'Kim', 1021: 'Ani', 1022: 'Mishka'}


print(employee.get(1023))
Answer:
None

Q18.
dict = {}
dict["Name"] = ["Jack"]
dict["Marks"] = [45]
print(dict)

Answer:
{'Name': ['Jack'], 'Marks': [45]}

Q19.

dict1 = {n:n*3 for n in range(6)}


print(dict1)

Answer:
{0: 0, 1: 3, 2: 6, 3: 9, 4: 12, 5: 15}

Q20.

my_dict = {"name": "Harry", "roll": "23", "marks": "64"}


if "marks" in my_dict:
print("Yes, 'marks' is one of the keys in dictionary")
Answer:
Yes, 'marks' is one of the keys in dictionary

Q21.

my_dictionary = {"tim": 11, "John": 22, "nick": 23}


maximum_key = max(my_dictionary,
key=my_dictionary.get)
print(maximum_key)

Answer:
nick

Q22.

my_dictionary = {"tim": 111, "John": 222, "nick": 223}


minimum_key = min(my_dictionary,
key=my_dictionary.get)
print(minimum_key)

Answer:
tim
Q23.
food = {'Nusha': 'Burger'}
food['Nusha'] = [4]
food['Nusha'].append('Pizza')
print(food)

Answer:
{'Nusha': [4, 'Pizza']}

Q23.

name = ['Tom', 'Mack', 'Jim']


dictionary = dict.fromkeys(name, 'pass') #Convert
list to dictionary python

print(dictionary)

Answer:
{'Tom': 'pass', 'Mack': 'pass', 'Jim': 'pass'}
Q24.

my_dict = {[4, 6]: 'Python'}


print(my_dict)

Answer:
TypeError: unhashable type: ‘list

Q25.

my_dict = {}
my_dict["marks"] = [12, 22, 26]
print(my_dict)

Answer:
{'marks': [12, 22, 26]}

Q26.

food = [('Burger', 2), ('Sandwich', 5), ('Pizza', 10)]


dictionary = dict((k,v) for (k,v) in food)
print(f'dictionary: {dictionary}')

Answer:
dictionary: {'Burger': 2, 'Sandwich': 5, 'Pizza': 10}

Q27.
employee = {201: 'Chris', 202: 'Kathreen', 203:
'Mishal'}
for r in employee:
print(r, ':',employee[r])

Answer:
dictionary: {'Burger': 2, 'Sandwich': 5, 'Pizza': 10}

Q28.

list1 = ['Red', 'Blue', 'Pink', 'Green']


list2 = ['4', '5', '8', '12', '10']
color_dictionary = dict(zip(list1, list2))
print(color_dictionary)

Answer:
{'Red': '4', 'Blue': '5', 'Pink': '8', 'Green': '12'}

Q29.

my_key = ["Paras", "Jain", "Cyware"]


my_value = ["101", "102", "103"]
result = {k:v for k,v in zip(my_key,my_value)}
print(result)

Answer:
{'Paras': '101', 'Jain': '102', 'Cyware': '103'}

Q30.
my_value = {30: 'Tom', 18: 'Anisha', 20: 'Jim'}
store = dict(sorted(my_value.items()))
print("sorting by value: ", store)
Answer:

sorting by value: {18: 'Anisha', 20: 'Jim', 30: 'Tom'}

Q31.
food = {
'cheese sandwich': 22,
'Butter sandwich': 15,
'Paneer sandwich': 19
}
des_orders = sorted(food.items(), reverse=True)
print(des_orders)
for i in des_orders:
print(i[0], i[1])

Q32.
dict1 = {"item_id":101,"color":
["Black","White"],"price":99}
input_color = input("Please fill in your color: ")
if input_color in dict1.get("color"):
print("Price = ",dict1.get("price"))
else: print("your color is not supported")

Answer:
Please fill in your color: White
Price = 99

Q33.

Write a Python Program to Remove Given Key from a Dictionary

myDict = {'name': 'John', 'age': 25, 'job': 'Developer'}


print("Dictionary Items : ", myDict)

key = input("Please enter the key that you want to Delete : ")

Q34.

Create a to Create Dictionary of Numbers 1 to n in (x, x**2) form

number = int(input("Please enter the Maximum Number : "))


myDict = {}

Q35.
Write a python program to add key-value pair to a dictionary
key = input("Please enter the Key : ")
value = input("Please enter the Value : ")

myDict = {}

You might also like