![]() |
removing elements from a list that are in another list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: removing elements from a list that are in another list (/thread-4129.html) |
removing elements from a list that are in another list - Skaperen - Jul-25-2017 i have a big list and another list. i'd like find an expression which yields a new list which has all the elements of the big list except any that are in the other list. the code i can use works in python versions 2.5, 2.6, 2.7, 3.3, 3.4, 3.5 and 3.6 and does not require any add-on packages (e.g. only uses what comes in the version of python). anything that is imported must import the same name for all versions. a plus: tuples will work (can yield a tuple or a list) and a mix (a list and a tuple) will work. RE: removing elements from a list that are in another list - ichabod801 - Jul-25-2017 If the items are unique and the order is unimportant, I would use sets: new_list = set(big_list) - set(other_list)Otherwise a list comprehension would work: new_list = [item for item in big_list if not item in set(other_list)] RE: removing elements from a list that are in another list - Skaperen - Jul-25-2017 (Jul-25-2017, 01:49 AM)ichabod801 Wrote: If the items are unique and the order is unimportant, I would use sets:yes, they are unique and the order is unimportant. thanks! |