Dictionaries Examples
Dictionaries Examples
Q1.
sortFruitSet = ['T', 'M', 'D', 'L', 'A']
sortFruitSet.sort()
print("Sorted List = ", sortFruitSet)
Answer:
Sorted List = ['A', 'D', 'L', 'M', 'T']
Answer:
Sorted List = ['T', 'M', 'L', 'D', 'A']
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'}
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.
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.
Q15.
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.
Q18.
dict = {}
dict["Name"] = ["Jack"]
dict["Marks"] = [45]
print(dict)
Answer:
{'Name': ['Jack'], 'Marks': [45]}
Q19.
Answer:
{0: 0, 1: 3, 2: 6, 3: 9, 4: 12, 5: 15}
Q20.
Q21.
Answer:
nick
Q22.
Answer:
tim
Q23.
food = {'Nusha': 'Burger'}
food['Nusha'] = [4]
food['Nusha'].append('Pizza')
print(food)
Answer:
{'Nusha': [4, 'Pizza']}
Q23.
print(dictionary)
Answer:
{'Tom': 'pass', 'Mack': 'pass', 'Jim': 'pass'}
Q24.
Answer:
TypeError: unhashable type: ‘list
Q25.
my_dict = {}
my_dict["marks"] = [12, 22, 26]
print(my_dict)
Answer:
{'marks': [12, 22, 26]}
Q26.
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.
Answer:
{'Red': '4', 'Blue': '5', 'Pink': '8', 'Green': '12'}
Q29.
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:
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.
key = input("Please enter the key that you want to Delete : ")
Q34.
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 = {}