Ask Question
18 October, 14:19

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.

+3
Answers (1)
  1. 18 October, 15:02
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers