I want to be able to reverse the list with a set of values in a dictionary. However some of the values are duplicates and when its brought into reversing the list, it only uses the same value.
my_list = (1, 2, 3, 3, 4)
my_dict = {'Apple': 1, 'Orange': 2, 'Banana': 3, 'Kiwi': 3, 'Pear': 4}
I have it so that if I want the list to be saying the fruits, it will reverse the dictionary and print out the fruits
new_list = []
for a in my_list:
reversed_dict = {my_dict[k]:k for k in my_dict}
fruit = [reversed_dict[elem] for elem in a]
new_list.append(fruit)
This will print out the list as: [Apple, Orange, Kiwi, Kiwi, Pear)
I want it to be able to check for duplicates and since 3 is in the list twice, if the same option (Kiwi) has already been said, then switch it to the other key in the dictionary. So ideally it would show.
This will print out the list as: [Apple, Orange, Banana, Kiwi, Pear)