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': []}
# 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))
Comments
Leave a comment