Dictionaries in Python (1)
Dictionaries in Python (1)
value
# Sort the items (key-value pairs) in the dictionary 'd' based on the
values (1st element of each pair).
# The result is a list of sorted key-value pairs.
sorted_d = sorted(d.items(), key=operator.itemgetter(1))
Sample Output:
Copy
Sample Output:
Write a Python script to concatenate the following dictionaries to create a new one.
Visual Presentation:
Sample Solution:
Python Code:
# Update 'dic4' by adding the key-value pairs from the current dictionary
'd'. dic4.update(d)
# Print the combined dictionary 'dic4' containing all the key-value pairs
from 'dic1', 'dic2', and 'dic3'. print(dic4)
Copy
Sample Output:
def is_key_present(x):
else:
# If 'x' is not present in 'd', print a message indicating that the key
is not present.
is_key_present(5)
is_key_present(9)
Copy
Sample Output:
Sample Solution:
Python Code:
# Iterate through the key-value pairs in the dictionary using a for loop.
# 'dict_key' represents the key, and 'dict_value' represents the value
for each pair.
Copy
Sample Output:
x -> 10
y -> 20
z -> 30
Python: Generate and print a dictionary that
contains a number in the form (x, x*x)
Write a Python script to generate and print a dictionary that contains a number (between 1
and n) in the form (x, x*x).
# Prompt the user to input a number and store it in the variable 'n'.
d = dict()
# Calculate the square of each number and store it in the dictionary 'd'
with the number as the key.
d[x] = x * x
Sample Output:
10
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Python: Sum all the items in a dictionary
Write a Python program to sum all the items in a dictionary.
Sample Solution:
Python Code:
# Use the 'sum' function to calculate the sum of all values in the
'my_dict' dictionary.
result = sum(my_dict.values())
Sample Output:
293
Python Code:
# Iterate through the key-value pairs in the dictionary 'd' using a for
loop.
# Print the color name, 'corresponds to', and its corresponding numerical
value.
Sample Output:
Red corresponds to 1
Green corresponds to 2
Blue corresponds to 3
Python Code:
d = dict()
# Calculate the square of each number and store it in the dictionary 'd'
with the number as the key.
d[x] = x ** 2
print(d)
Sample Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11:
121, 12: 144, 13: 169, 14: 196, 15:
225}
d = d1.copy()
print(d)
Sample Output:
result = 1 # Iterate through the keys in the 'my_dict' using a for loop.
for key in my_dict:
# Multiply the current 'result' by the value associated with the current
key in 'my_dict'.
# Print the final 'result,' which is the product of all values in the
dictionary.
print(result)
Sample Output:
-1333800
Python: Remove a key from a dictionary
Write a Python program to remove a key from a dictionary.
Visual Presentation:
Sample Solution:
Python Code:
if 'a' in myDict:
# If 'a' is in the dictionary, delete the key-value pair with the key
'a'.
del myDict['a']
# Print the updated dictionary 'myDict' after deleting the key 'a' (if it
existed).
print(myDict)
Sample Output:
# Use the 'zip' function to pair each color name with its corresponding
color code and create a list of tuples.
# Then, use the 'dict' constructor to convert this list of tuples into a
dictionary 'color_dictionary'.
print(color_dictionary)
Sample Output:
Python Code:
Sample Output:
black: #000000
green: #008000
red: #FF0000
white: #FFFFFF
Python Code:
# Find the key with the maximum value in 'my_dict' using the 'max'
function and a lambda function.
key=(lambda k: my_dict[k]))
# Find the key with the minimum value in 'my_dict' using the 'min'
function and a lambda function.
Sample Output:
class dictObj(object):
def __init__(self):
test = dictObj()
# Print the '__dict__' attribute of the 'test' object, which contains its
attribute-value pairs.
print(test.__dict__)
Sample Output:
Visual Presentation:
Sample Solution:
Python Code:
result[key] = value
Python Code:
import Counter
# Use the 'Counter' class to create counter objects for 'd1' and 'd2',
which count the occurrences of each key.
# Then, add the counters together to merge the key-value pairs and their
counts.
d = Counter(d1) + Counter(d2)
# Print the resulting merged dictionary 'd'. print(d)
Sample Output:
Sample Output: