Ask Question

Write a Python function shared_values that takes a dictionary where the keys are strings, and the values are integer lists. The function will return a new dictionary, where for each key/value pair, the key is an integer, and the value is a list of strings. The keys of the resulting dictionary will correspond to the values in the original dictionary, and the values will be the keys from the original dictionary that shared a value in common.

+4
Answers (1)
  1. 8 May, 09:53
    0
    Python code explained below

    Explanation:

    CODE:

    def shared_values (d):

    key = list (d. keys ())

    val = []

    # Collecting all values in a list irrespective of key

    for k in key:

    for v in d[k]:

    if not v in val:

    val. append (v)

    #Creating new dictionary

    new_d={}

    for i in val:

    for j in key:

    if i in d[j]:

    if i in new_d. keys ():

    new_d[i]. append (j)

    else:

    new_d[i] = [j]

    return new_d

    print (shared_values ({'a':[3,11,2],'b':[2,3,7],'c':[5,7]}))
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a Python function shared_values that takes a dictionary where the keys are strings, and the values are integer lists. The function ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers