0% found this document useful (0 votes)
10 views5 pages

Dictionary Practice Questions Python Coding

The document provides a series of exercises focused on Python dictionary operations, including adding, modifying, merging, and accessing values. It covers various techniques such as using the zip function, updating dictionaries, counting character frequencies, and manipulating nested dictionaries. Each exercise includes code examples demonstrating the concepts discussed.

Uploaded by

elumalaikalai05
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)
10 views5 pages

Dictionary Practice Questions Python Coding

The document provides a series of exercises focused on Python dictionary operations, including adding, modifying, merging, and accessing values. It covers various techniques such as using the zip function, updating dictionaries, counting character frequencies, and manipulating nested dictionaries. Each exercise includes code examples demonstrating the concepts discussed.

Uploaded by

elumalaikalai05
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

UCSC213E PYTHON PROGRAMMING FOR ANALYTICS

Python Dictionary Practice Program


Python Programming Questions
Exercise 1: Perform basic dictionary operations
my_dict = {'name': 'Alice', 'age': 35, 'city': 'New York'}
print(f"Original dictionary: {my_dict}")
# Add a new key-value pair
my_dict['profession'] = 'Doctor'
print(f"Updated dictionary after adding 'profession': {my_dict}")
# Modify Value
my_dict['age'] = 40
print(f"Updated dictionary after modification: {my_dict}")
# print key
print('City:', my_dict['city'])

Exercise 2: Dictionary from Lists


Solution 1: The zip() function and a dict() constructor
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]

res_dict = dict(zip(keys, values))


print(res_dict)
Solution 2: Using a loop and update() method of a dictionary
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
# empty dictionary
res_dict = dict()
for i in range(len(keys)):
res_dict.update({keys[i]: values[i]})
print(res_dict)

Exercise 3: Merge two Python dictionaries into one


dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict3 = [Link]()
[Link](dict2)
print(dict3)

Exercise 4: Count Character Frequencies


ef count_char_frequencies(input_string):
frequency_dict = {}
for char in input_string:
# Use get() method: if char is in dict, get its value; otherwise, default to 0
frequency_dict[char] = frequency_dict.get(char, 0) + 1
return frequency_dict
string1 = 'Jessa'
print(f"Frequencies for '{string1}': {count_char_frequencies(string1)}")

Exercise 5: Access Nested Dictionary


nested_person_dict = {'person': {'name': 'Alice', 'age': 30}}
print(f"Nested dictionary: {nested_person_dict}")

# Access Alice's age


# 1. Access the 'person' key to get the inner dictionary
# 2. Access the 'age' key within that inner dictionary
alices_age = nested_person_dict['person']['age']
print(f"Alice's age is: {alices_age}")
Exercise 6: Print the value of key ‘history’ from nested dict
sampleDict = {
"class": {
"student": {
"name": "Mike",
"marks": {
"physics": 70,
"history": 80
}
}
}
}

# understand how to located the nested key


# sampleDict['class'] = {'student': {'name': 'Mike', 'marks': {'physics': 70, 'history': 80}}}
# sampleDict['class']['student'] = {'name': 'Mike', 'marks': {'physics': 70, 'history': 80}}
# sampleDict['class']['student']['marks'] = {'physics': 70, 'history': 80}

# solution
print(sampleDict['class']['student']['marks']['history'])

Exercise 7: Rename key of a dictionary


sample_dict = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"
}
sample_dict['location'] = sample_dict.pop('city')
print(sample_dict)

Exercise 8: Change value of a key in a nested dictionary


sample_dict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 6500}
}
sample_dict['emp3']['salary'] = 8500
print(sample_dict)

Exercise 9: Get the key of a minimum value


sample_dict = {
'Physics': 82,
'Math': 65,
'history': 75
}
print(min(sample_dict, key=sample_dict.get))

Exercise 10: Delete a list of keys from a dictionary


sample_dict = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"
}
# Keys to remove
keys = ["name", "salary"]

for k in keys:
sample_dict.pop(k)
print(sample_dict)

You might also like