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?
Getting unique elements from List
Reference
To be consistent with the type I would use:
The example you provided does not correspond to lists in Python. It resembles a nested dict, which is probably not what you intended.
A Python list:
To get unique items, just transform it into a set (which you can transform back again into a list if required):
use set to de-duplicate a list, return as list
Alternatively in Python 3.6+: