How to get a unique value from a list in python

We Are Going To Discuss About How to get a unique value from a list in python. So lets Start this Python Article.

How to get a unique value from a list in python

  1. How to solve How to get a unique value from a list in python

    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']

  2. How to get a unique value from a list in python

    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']

Solution 1

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']

Original Author juanpa.arrivillaga Of This Content

Solution 2

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.”

Original Author Saso Of This Content

Solution 3

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.

Original Author Paul P Of This Content

Solution 4

  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']

Original Author Its Fragilis Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment