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
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 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 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 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 1,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,912 Aug-24-2022, 05:26 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,501 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,014 May-17-2022, 11:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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