Get Unique Values from a List in Python

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

How to get only distinct values from a list?

This is what you needed with set():

>>> lst1 = ['A','A','A','B','C','C','D','D','D','B','B']
>>> list(set(lst1))
['A', 'B', 'D', 'C']

Another solution OrderedDict to keep the order of keys during insertion.

>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(lst1))
['A', 'B', 'C', 'D']

In case you have liberty to use pandas then try below ones..

>>> import pandas as pd
>>> drop_dups = pd.Series(lst1).drop_duplicates().tolist()
>>> drop_dups
['A', 'B', 'C', 'D']

In case you are looking for common values between two files:

$ cat getcomn_vals.py
#!/python/v3.6.1/bin/python3
def print_common_members(a, b):
"""
Given two sets, print the intersection, or "No common elements".
Remove the List construct and directly adding the elements to the set().
Hence assigned the dataset1 & dataset2 directly to set()
"""

print('\n'.join(s.strip('\n') for s in a & b) or "No common element")

with open('file1.txt') as file1, open('file2.txt') as file2:
dataset1 = set(file1)
dataset2 = set(file2)
print_common_members(dataset1, dataset2)

Add only unique values to a list in python

To eliminate duplicates from a list, you can maintain an auxiliary list and check against.

myList = ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and', 
'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light',
'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the',
'through', 'what', 'window', 'with', 'yonder']

auxiliaryList = []
for word in myList:
if word not in auxiliaryList:
auxiliaryList.append(word)

output:

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 
'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick',
'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']

This is very simple to comprehend and code is self explanatory. However, code simplicity comes on the expense of code efficiency as linear scans over a growing list makes a linear algorithm degrade to quadratic.


If the order is not important, you could use set()

A set object is an unordered collection of distinct hashable objects.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

Since the average case for membership checking in a hash-table is O(1), using a set is more efficient.

auxiliaryList = list(set(myList))

output:

['and', 'envious', 'already', 'fair', 'is', 'through', 'pale', 'yonder', 
'what', 'sun', 'Who', 'But', 'moon', 'window', 'sick', 'east', 'breaks',
'grief', 'with', 'light', 'It', 'Arise', 'kill', 'the', 'soft', 'Juliet']

Find the indexes of unique elements of a list in python?

You can use built-in types for this.

CODE

l=[1,2,2,3,4,5,5,5]

indexes = [l.index(x) for x in set(l)]

EXPLANATION

  • set

    All unique members of the list.
  • list.index

    Returns the first index of an element.

COMMENT

As pointed out in the comments, if order is important for you, you can either use sorted on the set or on the resulting index list depending on the data that is provided. If the data is already sorted, I would suggest to do it like this:

indexes = [l.index(x) for x in sorted(set(l))]

How to make lists contain only distinct element in Python?

The simplest is to convert to a set then back to a list:

my_list = list(set(my_list))

One disadvantage with this is that it won't preserve the order. You may also want to consider if a set would be a better data structure to use in the first place, instead of a list.

How to get unique values from list in specific order?

This should do it:

L1 = ['r-A','r-G','S1','r-A','S2','r-O','r-G','S2','S1','r-A']
L2 = []
for item in L1:
if 'r-' in item and item in L2:
continue
else:
L2.append(item)
print(L2)

Output:

['r-A', 'r-G', 'S1', 'S2', 'r-O', 'S2', 'S1']


Related Topics



Leave a reply



Submit