Implement the function flip_dict which takes in a dictionary and returns a similar dictionary where the values have become the keys and the keys have become the values.
In this problem, you can assume that each key value pair is unique, and no key or value will be repeated or used more than once.
def flip_dict(dictionary):
"""Returns a flipped version of the original dictionary.
>>> TAs = {"12pm-2pm": "brian", "10am-12pm": "sophia", "2pm-4pm": "alec"}
>>> flipped_TAs = flip_dict(TAs)
>>> sorted_keys = sorted(flipped_TAs)
>>> sorted_keys ['alec', 'brian', 'sophia']
>>> [flipped_TAs[i] for i in sorted_keys] ['2pm-4pm', '12pm-2pm', '10am-12pm'] """
"*** YOUR CODE HERE ***"
The python code to invert dictionary can be written as:

You can copy the code from below text and use the indentation as shown above.
def flip_dict(dictionary):
# initialize result as empty dictionary
result = {}
# consider each key from input dictioanry
for key in dictionary:
# get value of key
val = dictionary[key]
# add inverted key value pairs to result
result[val] = key
# return inverted dictionary
return result
# test the function
TAs = {"12pm-2pm": "brian", "10am-12pm": "sophia", "2pm-4pm":
"alec"}
flipped_TAs = flip_dict(TAs)
# display flipped dictionary
print(flipped_TAs)
Output of above code:
{'brian': '12pm-2pm', 'sophia': '10am-12pm', 'alec': '2pm-4pm'}
If you have any doubts then you can ask in comment section. Thank you.
Implement the function flip_dict which takes in a dictionary and returns a similar dictionary where the...
python Define a function called print_values which takes a dictionary object as a parameter. The function should print all values in the dictionary. However, the order is based on the sorted keys in the dictionary. For example, if we have the following dictionary: {'b':36, 'a':12, 'c':24} The output is: 12 36 24 A faulty solution has been provided below. Identify the fault and submit a corrected version of this code. def print_values(dict1): for k in list(dict1.keys()).sort(): print(dict1[k], end=" ") For...
Using Python, Implement a function that returns an opposite of a dictionary: In order to decrypt the encrypted text, we will use the opposite of the substitution dictionary. Opposite dictionary of a dictionary is the dictionary where each value is the key and the key is the value. For instance, for the dictionary above, its opposite dictionary is {'I': 'A', 'T': 'B', 'R': 'C', 'A': 'D', 'N': 'E', 'S': 'F', 'L': 'G', 'O': 'H', 'M': 'I', 'W': 'J', 'U': 'K',...
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...
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 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...
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...
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...
1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are printed in alphabetical order. Note that...
USE PYTHON / RECURSION METHOD
Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...
# 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are...