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.Constraints:Input Format:Output Format: Example:Input:{"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}["name", "salary"]Output:{'name': 'Kelly', 'salary': 8000}Explanation:Dictionary = {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}Keys that needs to be extracted = ["name", "salary"]Result = {'name': 'Kelly', 'salary': 8000}Public Test Cases:# INPUT EXPECTED OUTPUT1 {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"} ["name", "salary"] {'name': 'Kelly', 'salary': 8000}
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.Constraints:Input Format:Output Format: Example:Input:{"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}["name", "salary"]Output:{'name': 'Kelly', 'salary': 8000}Explanation:Dictionary = {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}Keys that needs to be extracted = ["name", "salary"]Result = {'name': 'Kelly', 'salary': 8000}Public Test Cases:# INPUT EXPECTED OUTPUT1 {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"} ["name", "salary"]
{'name': 'Kelly', 'salary': 8000}
Solution
Here is a Python program that creates a new dictionary by extracting the specified keys from an existing dictionary:
def extract_keys(dictionary, keys):
new_dict = {key: dictionary[key] for key in keys}
return new_dict
# Test the function
dictionary = {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}
keys = ["name", "salary"]
print(extract_keys(dictionary, keys))
In this program, we define a function extract_keys that takes a dictionary and a list of keys as input. It uses a dictionary comprehension to create a new dictionary that only includes the specified keys and their corresponding values.
The function is then tested with the dictionary and keys provided in the question. The output of the function is printed to the console.
Similar Questions
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.
Write a Python Program to add a key to a dictionary.Sample Test CasesTest Case 1:Expected Output:Enter·the·number·of·key-value·pairs:·3Enter·the·key:·1Enter·the·value:·23Enter·the·key:·2Enter·the·value:·24Enter·the·key:·3Enter·the·value:·25Enter·the·new·key:·4Enter·the·new·value:·987987{'1':·'23',·'2':·'24',·'3':·'25',·'4':·'987987'}
map dictionaryWrite a Python program to map two lists into a dictionary.Sample Test CasesTest Case 1:Expected Output:Enter·the·number·of·elements:·2Enter·key:·1Enter·value:·20Enter·key:·2Enter·value:·30The·dictionary·mapping·is:{'1':·'20',·'2':·'30'}Test Case 2:Expected Output:Enter·the·number·of·elements:·1Enter·key:·1Enter·value:·abcThe·dictionary·mapping·is:{'1':·'abc'}
Write a program that prints all the keys and values in a dictionary where the key is "dennis" and the value is "student."
Evaluate the given code snippet. Ignoring the order, which line of code will you use in the place of "# LINE A" to generate the output shown here ?keys = ['key1', 'key2', 'key3', 'key4', 'key5'] vals = ['val1', 'val2', 'val3', 'val4', 'val5'] # LINE Aprint(mydict)# Output: {'key1': 'val1', 'key2': 'val2', 'key3': 'val3', 'key4': 'val4', 'key5': 'val5'}Select one:mydict = dict()for x in keys: mydict[x] = vals.pop()mydict = dict(keys.join(vals))mydict = dict(keys, vals)mydict = dict()mydict.fromkeys(keys, vals)mydict = dict()for x in range(len(keys)): mydict[keys[x]] = vals[x]
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.