Christopher Hatton
Christopher Hatton

Reputation: 63

How to get a unique value from a list in python

Imagine I have this list:

list = ['a','a','b','a']

I would like to do something like this:

print(unique(list))

to retrieve the unique item(s), so python will output b. How could I do this?

Upvotes: 5

Views: 13985

Answers (5)

Gulzar
Gulzar

Reputation: 27946

Most straight forward way, use a dict of counters.

a = ['a','a','b','a']

counts = {}
for element in a:
    counts[element] = counts.get(element, 0) + 1

for element in a:
    if counts[element] == 1:
        print(element)

out:

b

Upvotes: 0

Saso
Saso

Reputation: 74

Using set() property of Python, we can easily check for the unique values. Insert the values of the list in a set. Set only stores a value once even if it is inserted more then once. After inserting all the values in the set by list_set=set(list1), convert this set to a list to print it.

for more ways check : https://www.geeksforgeeks.org/python-get-unique-values-list/#:~:text=Using%20set()%20property%20of,a%20list%20to%20print%20it.

Example to make a unique function :

# Python program to check if two
# to get unique values from list
# using set

# function to get unique values
def unique(list1):
 
    # insert the list to the set
    list_set = set(list1)
    # convert the set to the list
    unique_list = (list(list_set))
    for x in unique_list:
        print x,
 

# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)


list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)

output should be: the unique values from 1st list is 40 10 20 30 the unique values from 2nd list is 1 2 3 4 5

You can also use numpy.unique example:

`
#Ppython program to check if two # to get unique values from list # using numpy.unique import numpy as np

# function to get unique values
def unique(list1):
    x = np.array(list1)
    print(np.unique(x))
 

# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)


list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)

` output should be : the unique values from 1st list is [10 20 30 40]

the unique values from 2nd list is [1 2 3 4 5]

Fore more please check "https://www.geeksforgeeks.org/python-get-unique-values-list/#:~:text=Using%20set()%20property%20of,a%20list%20to%20print%20it."

Upvotes: 2

Its Fragilis
Its Fragilis

Reputation: 208

  1. Count the appearance of an element in the list, and store it in a dictionary
  2. From the dictionary, check which elements has only one appearance
  3. Store the unique (one appearance) elements in a list
  4. Print the list

You can try this:

my_list = ['a','a','b','a']
my_dict = {}
only_one = []

#Step 1
for element in my_list:
  if element not in my_dict:
    my_dict[element] = 1
  else:
    my_dict[element] += 1

#Step 2 and Step 3
for key, value in my_dict.items():
  if value == 1:
    only_one.append(key)

#Step 4
for unique in only_one:
  print(unique)

Output:

b

In case you are wondering what other variables contain:

my_dict = {'a': 3, 'b': 1}
only_one = ['b']

Upvotes: 0

Paul P
Paul P

Reputation: 3907

You could use collections.Counter() for this, e.g.:

from collections import Counter

my_list = ['a','a','b','a']

counts = Counter(my_list)

for element, count in counts.items():
    if count == 1:
        print(element)

would print b and nothing else in this case.

Or, if you'd like to store the result in a list:

from collections import Counter

my_list = ['a','a','b','a']

counts = Counter(my_list)

unique_elements = [element for element, count in counts.items() if count == 1]

unique_elements is ['b'] in this case.

Upvotes: 1

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

Count the items, keep only those which have a count of 1:

>>> data = ['a','a','b','a']
>>> from collections import Counter
>>> [k for k,v in Counter(data).items() if v == 1]
['b']

Upvotes: 8

Related Questions