Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.
Question
Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.
Solution
Sure, here is a Python program that extracts specified keys from a dictionary and creates a new dictionary with those keys.
# Original dictionary
original_dict = {
'name': 'John',
'age': 25,
'job': 'Engineer',
'city': 'New York'
}
# Keys to extract
keys_to_extract = ['name', 'job']
# Create a new dictionary with the extracted keys
new_dict = {key: original_dict[key] for key in keys_to_extract}
# Print the new dictionary
print(new_dict)
In this program, we first define the original dictionary and the keys we want to extract. Then, we use a dictionary comprehension to create a new dictionary that only includes the specified keys and their corresponding values. Finally, we print the new dictionary.
Similar Questions
Write a Python program to get a dictionary value from the user and count the number of keys that are having the same value and print the count.
Write a program that prints all the keys and values in a dictionary where the key is "dennis" and the value is "student."
Given a Python dictionary d and a value v, it is efficient to find the corresponding key: d[k] = v.Question 27Select one:TrueFalse
Given a Python dictionary d and a value v, it is efficient to find the corresponding key: d[k] = v.Question 5Select one:TrueFalse
Python Dictionary is used to store the data in a ___ format.Key value pairGroup value pairSelect value pairNone of the mentioned above
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.