ordered = [item for item in ordered if item in unordered]
This method creates a new list based on old ones using Python list comprehension.
For large amounts of data, turning an unordered list into a set first, as the people suggested in the comments, is of utmost importance in performance, for example:
unordered = set(unordered)
Benchmark!
ordered: 5000 items, unordered: 1000 items
0.09561 s without dial 0.00042 with setting
For 10/2 elements, the time is almost the same, therefore it is useful to always use a set, regardless of the size of the data.
source share