2

I have a list with several words and I want to print the unique words from that list. With "unique" words I mean words that only appear once in my original list. That is, if a word appears twice (or more than twice), then it shouldn't be printed.

Here's my list of words:

my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]

And here's the code I've tried so far:

print(set(my_list))

However, set prints a list with ALL the words, though there aren't duplicates. That is: door, table, chair, couch, closet. However, what I want is a list like chair, couch, closet(because they are the ones that appear only once in the list).

Me All
  • 269
  • 1
  • 5
  • 17
  • 4
    Possible duplicate of [how to print elements which occur only once in a list, without counting](https://stackoverflow.com/questions/17218789/how-to-print-elements-which-occur-only-once-in-a-list-without-counting) – Chris Beard Oct 31 '18 at 03:37

6 Answers6

1

Not a neat way to achieve what you want, but still a good workaround, by using Counter. Counter will count how many times each item appear in a list.

from collections import Counter
my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]
my_list_count = Counter(my_list) # Counter({'door': 3, 'table': 2, 'chair': 1, 'closet': 1, 'couch': 1})

# Unique item have count = 1
print([xx for xx in my_list_count if my_list_count[xx] == 1])
# Results: ['chair', 'closet', 'couch']
enamoria
  • 896
  • 2
  • 11
  • 29
1

You can use something like -

res = [x for x in my_list if my_list.count(x) == 1]

It will return list of elements that occur once.

aquaman
  • 1,523
  • 5
  • 20
  • 39
0

You can use the Counter for this, which will create a dictionary with each unique word as the key and the respective occurrence count as the corresponding value. Then iterate through the dictionary to find the key with the value of 1. The sample code is as follows:

from collections import Counter

my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]

count_all = Counter(my_list)
for key, value in count_all.items():
    if 1 == value:
        print key
Andreas
  • 2,455
  • 10
  • 21
  • 24
0

Here's a short snippet on how you can go about it.

from collections import Counter
count_dict = Counter(my_list)

for k, v in count_dict.items():
    if v == 1:
        print(k)
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
0

Here is something you can try

def keep_unique(array):
    counts = {}
    for e in array:
        if e not in counts:
            counts[e] = 0
        counts[e] += 1
    return [k for (k, v) in counts.items() if v == 1]

print(keep_unique(["door", "table", "door", "chair", "couch", "door", "table", "closet"]))

The keep_unique method counts the number of occurrences of each element and then returns only the ones which appear only once.

lakshayg
  • 2,053
  • 2
  • 20
  • 34
0

A solution without Counter could be to sort your list and check if surrounding words are different:

my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]
my_list.sort()

unique = [word for i, word in enumerate(my_list) if my_list[i - 1] != word and my_list[i + 1] != word] 
print(unique)
# ['chair', 'closet', 'couch']
slider
  • 12,810
  • 1
  • 26
  • 42