Answer to Question #275895 in Python for Happpy

Question #275895

Assume, you have been given a dictionary named dict_1. The values of the dictionary will be a list or a tuple depending on the case of the dictionary key. If the key is in lower case, then the value will be in list format. If the key is in upper case, then the value will be in tuple format. Now, write a Python program that creates a new dictionary named “dict_primes” that contains only the prime numbers in the value. Then, finally, print the dictionary dict_primes. ============================================================================= Given dictionary1: {"a":[5,2,55,17],"P":(11,121,222),"B":(37,53,71),"c":[45,92,50]} Sample Output 1: dict_primes = {'a': [5, 2, 17], 'P': (11,), 'B': (37, 53, 71), 'c': []}


1
Expert's answer
2021-12-07T20:58:05-0500
# Python code to convert into dictionary 

  

def Convert(tup, di): 


    for a, b in tup: 


        di.setdefault(a, []).append(b) 


    return di 


      
# Driver Code     

tups = [("akash", 10), ("gaurav", 12), ("anand", 14),  


     ("suraj", 20), ("akhil", 25), ("ashish", 30)] 


dictionary = {} 


print (Convert(tups, dictionary))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS