Get all values from the dictionary and add them to a list but don

Python / Python Dictionary

812

Given Input:

speed = {'jan': 47, 'feb': 52, 'march': 47, 'April': 44, 'May': 52, 'June': 53, 'july': 54, 'Aug': 44, 'Sept': 54}

Expected Output:

[47, 52, 44, 53, 54]

Program:

speed = {'jan': 47, 'feb': 52, 'march': 47, 'April': 44, 'May': 52, 'June': 53,
         'july': 54, 'Aug': 44, 'Sept': 54}

print("Dictionary's values - ", speed.values())

speed_list = list()

# iterate dict values
for val in speed.values():
    # check if value not present in a list
    if val not in speed_list:
        speed_list.append(val)
print("unique list", speed_list)

Output:

Dictionary's values -  dict_values([47, 52, 47, 44, 52, 53, 54, 44, 54])
unique list [47, 52, 44, 53, 54]

This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.