Python Forum
removing elements from a list that are in another list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
removing elements from a list that are in another list
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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)]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  removing one list element without using its index paul18fr 7 1,206 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  Strange behavior list of list mmhmjanssen 3 1,631 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  unable to remove all elements from list based on a condition sg_python 3 1,717 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 1,657 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 2,649 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 3,398 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,795 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 4,953 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 3,473 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 3,720 Aug-24-2022, 05:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020