Open this example in the interactive library
Source code
events = ['a', 'b', 'a', 'c', 'b']
latest_first = dict.fromkeys(reversed(events))
print(list(reversed(latest_first)))
Sample output
['a', 'c', 'b']

Keep the last occurrence and its relative order using Python lists and dictionaries.
Open this example in the interactive library
events = ['a', 'b', 'a', 'c', 'b']
latest_first = dict.fromkeys(reversed(events))
print(list(reversed(latest_first)))
['a', 'c', 'b']