68

Possible Duplicate:
Get difference from two lists in Python

What is a simplified way of doing this? I have been trying on my own, and I can't figure it out. list a and list b, the new list should have items that are only in list a. So:

a = apple, carrot, lemon
b = pineapple, apple, tomato
new_list = carrot, lemon

I tried writing code, but every time it always returns the whole list a to me.

Community
  • 1
  • 1
Eliza
  • 701
  • 1
  • 5
  • 5

5 Answers5

73

You can write this using a list comprehension which tells us quite literally which elements need to end up in new_list:

a = ['apple', 'carrot', 'lemon']
b = ['pineapple', 'apple', 'tomato']

# This gives us: new_list = ['carrot' , 'lemon']
new_list = [fruit for fruit in a if fruit not in b]

Or, using a for loop:

new_list = []
for fruit in a:
    if fruit not in b:
        new_list.append(fruit)

As you can see these approaches are quite similar which is why Python also has list comprehensions to easily construct lists.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • Note that this removes all occurrences of elements in `b` in `a`. So if `a == [1, 1, 1, 2, 3, 4]` and `b == [1, 3]`, then `new_list == [2, 4]`, instead of e.g. `new_list == [1, 1, 2, 4]`. – user3389669 Nov 03 '21 at 07:56
39

You can use a set:

# Assume a, b are Python lists

# Create sets of a,b
setA = set(a)
setB = set(b)

# Get new set with elements that are only in a but not in b
onlyInA = setA.difference(b)

UPDATE
As iurisilvio and mgilson pointed out, this approach only works if a and b do not contain duplicates, and if the order of the elements does not matter.

Zulu
  • 8,765
  • 9
  • 49
  • 56
  • 1
    I guess this is the way to go, but it changes the list if it has duplicated strings. – iurisilvio Jul 11 '12 at 14:23
  • 1
    @iurisilvio: You are right. This approach works only if `a` and `b` only contain unique entries. In that case it would make even more sense to use a `set` for `a`,`b` anyways. But then this is probably the fastest approach. – Michael Schlottke-Lakemper Jul 11 '12 at 14:30
  • It also doesn't work if the order of the items matters, but that might not be the case here (+1 from me) – mgilson Jul 11 '12 at 14:33
  • 2
    I think that the conversion of `b` to a `set` is not necessary. You can also perform the difference using the original `list`. – rlar Nov 27 '20 at 11:16
  • @rlar is correct, I found the same to be true. – infiniteloop Jun 10 '21 at 17:53
13

You may want this:

a = ["apple", "carrot", "lemon"]
b = ["pineapple", "apple", "tomato"]

new_list = [x for x in a if (x not in b)]

print new_list
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
martin
  • 2,520
  • 22
  • 29
8

Would this work for you?

a = ["apple", "carrot", "lemon"]
b = ["pineapple", "apple", "tomato"]

new_list = []
for v in a:
    if v not in b:
        new_list.append(v)

print new_list

Or, more concisely:

new_list = filter(lambda v: v not in b, a)
Alex Wilson
  • 6,690
  • 27
  • 44
5

How about using sets (or the built in set since Sets was deprecated in 2.6)?

from sets import Set
a = Set(['apple', 'carrot', 'lemon'])
b = Set(['pineapple','apple','tomato'])
new_set =  a.difference(b)
print new_set

gives the output

Set(['carrot', 'lemon'])
istruble
  • 13,363
  • 2
  • 47
  • 52
StuGrey
  • 1,479
  • 9
  • 20