This question already has an answer here:
- Removing duplicates in lists 43 answers
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:
[u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
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?
To get unique values from your list use code below:
IMPORTANT: Approach above won't work if any of items in a list is not hashable which is case for mutable types, for instance list or dict.
That means that you have to be sure that
trends
list would always contains only hashable items otherwise you have to use more sophisticated code:Set is a collection of ordered and unique elements. So, you can use set as below to get a unique list:
what type is your output variable?
Python sets are what you just need. Declare output like this:
and you're ready to go adding elements with
output.add(elem)
and be sure they're unique.Warning: sets DO NOT preserve the original order of the list.
You can use sets. Just to be clear, I am explaining what is the difference between a list and a set. sets are unordered collection of unique elements.Lists are ordered collection of elements. So,
But: Do not use list/set in naming the variables. It will cause error: EX: Instead of use list instead of unicode_list in the above one.
Maintaining order:
Order doesn't matter:
If you are using numpy in your code (which might be a good choice for larger amounts of data), check out numpy.unique:
(http://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html)
As you can see, numpy supports not only numeric data, string arrays are also possible. Of course, the result is a numpy array, but it doesn't matter a lot, because it still behaves like a sequence:
If you really want to have a vanilla python list back, you can always call list().
However, the result is automatically sorted, as you can see from the above code fragments. Check out numpy unique without sort if retaining list order is required.