5

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?

4
  • do you mean sets? Or do you mean you only want to print a certain element from the list? Commented May 23, 2021 at 9:09
  • Hint: The count() method of lists. Commented May 23, 2021 at 9:09
  • @ItsFragilis They have a list and they want to return only the elements that appear once. Commented May 23, 2021 at 9:09
  • 2
    Unrelated, don't use the name list for a variable name in Python, as it overrides a built-in name (the list constructor). Same for dict, sum, and more. Commented May 23, 2021 at 9:11

5 Answers 5

8

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']
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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.

Comments

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

Comments

0

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

2 Comments

Those conditions can be replaced with : counts[element] = counts.get(element, 0) + 1.
@SorousHBakhtiary I will forever use it that way. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.