0

I have 2 lists such as this:

>>>first = ['hello', 'hey', 'hi', 'hey']
>>>second = ['hey', 'hi', 'hello']

I want a new list that contains the word that is not included in the second list. In this case:

odd_word = ['hey']

What is the quickest way of doing this? Thank you.

I tried using the method shown here: Get difference between two lists, but it gives me a blank list.

>>> odd = list(set(first) - set(second))
>>> odd
[]
Community
  • 1
  • 1
Arjun Vasudevan
  • 139
  • 1
  • 8

2 Answers2

4

You could use collections.Counter:

>>> from collections import Counter
>>> first = ['hello', 'hey', 'hi', 'hey']
>>> second = ['hey', 'hi', 'hello']
>>> odd_word = list((Counter(first) - Counter(second)).elements())
>>> print(odd_word)
['hey']
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
1

Do this.

odd_word = [s for s in first if s not in second]

This will give you duplicates if there are duplicate words in first that aren't in second. If you don't want the duplicates, do this instead.

odd_word = list({s for s in first if s not in second})
Sam Marinelli
  • 999
  • 1
  • 6
  • 16
  • I dont think this works to find duplicates. It uses `in`, so if theres 1 or if theres 100 instances of the string it returns true. It doesnt do any kind of counting. – Paul Rooney Feb 03 '17 at 03:13
  • I'm talking about duplicates in `first`, not duplicates in `second`. – Sam Marinelli Feb 03 '17 at 03:14
  • 1
    http://ideone.com/XOCglf I get an empty list. I'd expect `['hey']`. I don't think it matters which way round, all the strings are in both lists at least once, so `in` is not a valid way to find dupes, or am I missing something? – Paul Rooney Feb 03 '17 at 03:16
  • Words appearing in `second` should not appear in the result as specified in the question. I'm talking about words appearing more than once in `first` and not at all in `second`. – Sam Marinelli Feb 03 '17 at 03:18
  • 1
    In the OP's example there are no strings that are in first but not in seconds, unless you are counting the number of times each string appears, I admit the question is ambiguous on this point. I gave an example where it doesn't appear to work correctly but anyway I won't to press the point. – Paul Rooney Feb 03 '17 at 03:29