Question
Python 3

Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a lis
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def get_subset(a_dict, a_keys):
    result = {}
    for k in a_keys:
        if k in a_dict:
            result[k] = a_dict[k]
    return result


# Testing the function here. ignore/remove the code below if not required
print(get_subset({"puffin": 5, "corgi": 2, "three": 3}, ["three", "corgi"]))

{ three: 3, corgi: 2} Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which...

    HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which takes two arguments: both are a dictionary of strings (keys) to integers (values). Complete the function such that it updates the first dictionary as follows: • For key-value pairs in both dictionaries, sum them • For key-value pairs in the second dictionary only, insert them into the first • Make no changes to key-value pairs unique to the first dictionary These updates should be...

  • HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which...

    HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which takes two arguments: both are a dictionary of strings (keys) to integers (values). Complete the function such that it updates the first dictionary as follows: • For key-value pairs in both dictionaries, sum them • For key-value pairs in the second dictionary only, insert them into the first • Make no changes to key-value pairs unique to the first dictionary These updates should be...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings...

    Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...

  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys...

    Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...

  • PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

    PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in...

  • Write a decorator function in python that takes in 3 strings as arguments and returns them...

    Write a decorator function in python that takes in 3 strings as arguments and returns them in reverse order.

  • Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...

    Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT