I have two lists:
ordered = ['salat', 'baguette', 'burger', 'pizza']
unordered = ['pizza', 'burger']
Now I want to remove all entries from the ordered list, that are not in the unordered list while preserving the ordering.
How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This method creates a new list based on the old ones using Python’s list comprehension.
For large amounts of data, turning the unordered list into a set first, as people suggested in comments, makes a huge difference in performance, e.g.:
Benchmark!
For 10/2 items the time is almost the same, so it’s good to always use a set, no matter what the data size is.