2

I'm using python to try and remove items that intersection from another list. So below is what I have.

letter = ['a', 'a', 'i', 'd', 'e', 'i', 'a', 'b', 'b', 'c', 'o', 'g', 'a', 'f', 'f', 'i', 'g', 'i' ]
cons = ['b','c','d', 'f', 'g']

and what I want is to remove any letter in the cons list from the letter list but preserve everything else. So below is what I want to get.

 letter = ['a', 'a', 'i', 'e', 'i', 'a', 'o', 'a', i', 'i' ]

Below is what I have tried so far but it's not working.

for i in letter[:]:
    if i in cons: 
         letter.remove(i)
         cons.remove(i)

and...

list(set(x) - set(y))

I just want to remove the intersection of the lists and keep the duplicates from the first list that are not in the second list. Everything I've tried so far has removed those duplicates from the first list that I want to keep. Any help is greatly appreciated!

user2743
  • 1,423
  • 3
  • 22
  • 34

2 Answers2

9
>>> letter = ['a', 'a', 'i', 'd', 'e', 'i', 'a', 'b', 'b', 'c', 'o', 'g', 'a', 'f', 'f', 'i', 'g', 'i' ]
>>> cons = ['b','c','d', 'f', 'g']

>>> [x for x in letter if x not in cons]
['a', 'a', 'i', 'e', 'i', 'a', 'o', 'a', 'i', 'i']

a simple list comprehension will do the trick?

EDIT:

As ShadowRanger said, it would improve performance (mostly for larger data sets than these) to convert cons to a set:

cons = set(cons)

then to go into the list comp. This is better because sets are hashed and makes getting items/checking for items in it way faster

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
R Nar
  • 5,465
  • 1
  • 16
  • 32
  • 2
    And for efficiency, it would probably help to store `cons` as a `set`/`frozenset` (or if it's short, as a plain `str`, which is still linear scan time, but faster to membership test for individual characters than a `list` of single character strings). – ShadowRanger Nov 13 '15 at 22:22
  • true, i wasnt really worried about efficiency much in this but that would make a huge boost. edited to add that – R Nar Nov 13 '15 at 22:26
3

A list comprehension is better, but your original code works if we just remove one line:

>>> letter = ['a', 'a', 'i', 'd', 'e', 'i', 'a', 'b', 'b', 'c', 'o', 'g', 'a', 'f', 'f', 'i', 'g', 'i' ]
>>> cons = ['b','c','d', 'f', 'g']
>>> for i in letter[:]:
...     if i in cons:
...          letter.remove(i)
...
>>> letter
['a', 'a', 'i', 'e', 'i', 'a', 'o', 'a', 'i', 'i']
Jim K
  • 12,824
  • 2
  • 22
  • 51