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 !

4 Answers4

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)
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • No effort made from OP. Maybe it was a homework. – Elis Byberi Nov 18 '17 at 16:33
  • @ElisByberi: Please show me a link/text that stackoverflow has a problem with prorgamming homeworks??!!! – DRPK Nov 18 '17 at 16:38
  • @DRPK https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions I will have agree ElisByberi that the OP did not show any effort of his own. – user2390182 Nov 18 '17 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. –  Nov 18 '17 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. – Elis Byberi Nov 18 '17 at 16:47
  • @schwobaseggl: i know bro ... but i have a question... why did you answer him?! and that is not a original internal stackoverflow privac_policy link! thats just another question link with a lot of people's personal opinions...so?! – DRPK Nov 18 '17 at 16:48
  • @ElisByberi: i know what you mean....but stackoverflow is for everyone..such as kids and students..we can not force them to just ask a highlevel questions ..man! – DRPK Nov 18 '17 at 16:49
  • @ElisByberi Wound point... one could argue to provide a correct answer knowing remove- and pop- based solutions will be suggested. But at the end of the day, I don't mind helping out and this question is quick'n'easy rep ;) – user2390182 Nov 18 '17 at 16:53
  • Thank you for your answer. I just wanted to make the most simple question for a better understanding and occult any non important information. Thank you all for your help. –  Nov 18 '17 at 16:53
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]
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
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.

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
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.

Van Peer
  • 2,127
  • 2
  • 25
  • 35
  • 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 '17 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. – user2390182 Nov 18 '17 at 16:37
  • @SreeramTP agree! thanks! – Van Peer Nov 18 '17 at 16:39
  • @schwobaseggl thanks! you answered list comprehension, so.. – Van Peer Nov 18 '17 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 '17 at 16:48
  • @JoeIddon True, that's why I added the set conversion remark... which would make the comprehension `O(M+N)` – user2390182 Nov 18 '17 at 16:49