Python Forum

Full Version: removing elements from a list that are in another list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)]
(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!