In this python dictionary program, we will get the sum of unique elements from dictionary list values.
Steps to solve the program
- Take a dictionary with the value list as input and create an empty list.
- Use for loop to iterate over the value list of the dictionary.
- Add unique elements from the value list to the empty list using a nested for loop and an if statement.
- Get the sum of the list using sum().
- Print the output.
D1 = {'a':[6,7,2,8,1],'b':[2,3,1,6,8,10],'d':[1,8,2,6,9]}
l = []
for v in D1.values():
for num in v:
if num not in l:
l.append(num)
print("Sum: ",sum(l))
Output :
Sum: 46
reverse each string value in the dictionary and add an underscore before and after the Keys.