Get unique value from list in python [duplicate]

This article was translated from: Get unique values ​​from a list in python [duplicate]

This question already has an answer here: This question already has an answer here :

I want to get the unique values ​​from the following list: I want to get the unique values ​​from the following list :

[u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']

The output which I require is: The output I need is:

[u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']

This code works: This code works :

output = []
for x in trends:
    if x not in output:
        output.append(x)
print output

is there a better solution I should use? should I use a better solution ?


#1st Floor

Reference: https://stackoom.com/question/s7CA/ Get the unique value from the list in python-duplicate


#2nd Floor

what type is your output variable? what type is your output variable ?

Python sets are the Just the What you need. Python set is what you need. Declare output like this: Declare output like this :

output = set([]) # initialize an empty set

and you're ready to go adding elements with output.add(elem)and be sure they're unique. You are ready to add elements with and be sure they're unique .output.add(elem)

Warning: sets DO NOT preserve the original order of the list. Warning: sets DO NOT preserve the original order of the list .


#3rd floor

First declare your list properly, separated by commas. First declare your list properly, separated by commas. You can get the unique values ​​by converting the list to a set. You can get the unique values ​​by converting the list to a set .

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
myset = set(mylist)
print(myset)

If you use it further as a list, you should convert it back to list by doing: If you use it further as a list, you should convert it back to list by doing the following:

mynewlist = list(myset)

Another possibility, probably faster would be to use a set from the beginning, instead of a list. Another possibility, probably faster, is to use a set from the beginning instead of a list . Then your code should be: Then your code should be :

output = set()
for x in trends:
    output.add(x)
print(output)

As it has been pointed out, the sets do not maintain the original order. As already pointed out, the sets do not maintain the original order . You need SO IF, Should you look up the About at The ordered the SET . If you need, you should review ordered sets .


#4th floor

The example you provided does not correspond to lists in Python. The example you provided does not correspond to lists in Python . It resembles a nested dict, which is probably not what you intended. It is similar to a nested dictionary and may not be what you want.

A Python list: Python list:

a = ['a', 'b', 'c', 'd', 'b']

To get unique items, just transform it into a set (which you can transform back again into a list if required): To get the unique items, just convert it to a set (you can convert it back to the list if needed) :

b = set(a)
print b
>>> set(['a', 'b', 'c', 'd'])

#5th Floor

First thing, the example you gave is not a valid list. First , the example you gave is not a valid list .

example_list = [u'nowplaying',u'PBS', u'PBS', u'nowplaying', u'job', u'debate',u'thenandnow']

Suppose if above is the example list. Suppose if above is the example list . Then you can use the following recipe as give the itertools example doc that can return the unique values and preserving the order as you seem to require. Then, you can use the following formula as itertools example document that can return a unique value and preserve your It seems the order is needed. The iterable here is the example_list

from itertools import ifilterfalse

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in ifilterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

#6th floor

  1. At the begin of your code just declare your output list as empty: output=[] At the beginning of your code just declare your output list as empty :output=[]
  2. Instead of your code you may use this code trends=list(set(trends)) you can use this code to replace your codetrends=list(set(trends))
Published 0 original articles · praised 8 · 30,000+ views

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/105531646