2

I have a master list which regroups every ID I have.

master = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Among all of these ID, I want to exclude some of them. So I did a second list of ID which have to be exclude

exclude = [1, 4, 5]

In your opinion, what is the good operation to make for having :

master_exclude = [2, 3, 6, 7, 8, 9, 10]

If you know what I mean ?

Thank you !

2
  • Did you try anything before asking this question? Nov 18, 2017 at 16:32
  • @wwii Thank you! I was searching for a duplicate too. Nov 18, 2017 at 16:43

4 Answers 4

5

This conditional list comprehension will work:

master_exclude = [x for x in master if x not in exclude]

If these involved lists are larger, consider first converting exclude to a set in order to make the contains check more performant:

exclude = set(exclude)
9
  • No effort made from OP. Maybe it was a homework. Nov 18, 2017 at 16:33
  • @ElisByberi: Please show me a link/text that stackoverflow has a problem with prorgamming homeworks??!!!
    – DRPK
    Nov 18, 2017 at 16:38
  • @DRPK meta.stackoverflow.com/questions/334822/… I will have agree ElisByberi that the OP did not show any effort of his own. Nov 18, 2017 at 16:42
  • Not at all. I'm programing only for fun ! I'm learning the Framework Django and I had an issue about something.
    – user6089877
    Nov 18, 2017 at 16:43
  • @DRPK I do not want to, neither I can stop you from doing others' homeworks. Do what you want to do. I have the right to express my opinions. Nov 18, 2017 at 16:47
1

You can do something like this with a simple for loop.

master = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

exclude = [1, 4, 5]

for o in exclude:
    try:
        master.remove(o)
    except ValueError:
        pass

print(master) # [2, 3, 6, 7, 8, 9, 10]
2
  • If you set master = [1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] then after running this, only the first 1 will be removed: [1, 1, 2, 3, 6, 7, 8, 9, 10]
    – Joe Iddon
    Nov 18, 2017 at 16:44
  • Thanks to pointing it out. !!
    – Sreeram TP
    Nov 18, 2017 at 17:16
1

The list-comprehension is definitely the right way to go, but this can also be done with filter. Not the ideal solution, but is another thought:

master_exclude = list(filter(lambda i: i not in exclude, master))

which gives:

[2, 3, 6, 7, 8, 9, 10]

Or you could use a while and for-loop:

for i in exclude:
    while master.count(i) > 0:
        master.remove(i)

which gives the same result.

0

This will remove the element from the master list, which are in exclude

for i in exclude:
    master.remove(i)

Will not remove duplicates.

6
  • 1
    It will work fine if all the elements of exclude belongs to the master list. Other wise it will lead to ValueError
    – Sreeram TP
    Nov 18, 2017 at 16:35
  • 1
    ... and remove is an O(N) operation which makes this approach O(N*M) which is why it is better to build the new list from scratch. Nov 18, 2017 at 16:37
  • @SreeramTP agree! thanks!
    – Van Peer
    Nov 18, 2017 at 16:39
  • @schwobaseggl thanks! you answered list comprehension, so..
    – Van Peer
    Nov 18, 2017 at 16:40
  • 1
    @schwobaseggl wouldn't using a list-comprehension also be O(n*m) as testing not in still has to go through the exclude list. Of course it would depend on the relative sizes of the lists...
    – Joe Iddon
    Nov 18, 2017 at 16:48