How to sort and remove duplicates from Python list?
Question:
Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:
from sets import Set
[...]
myHash = Set(myList)
but I don’t know how to retrieve the list members from the hash in alphabetical order.
I’m not married to the hash, so any way to accomplish this will work. Also, performance is not an issue, so I’d prefer a solution that is expressed in code clearly to a fast but more opaque one.
Answers:
If it’s clarity you’re after, rather than speed, I think this is very clear:
def sortAndUniq(input):
output = []
for x in input:
if x not in output:
output.append(x)
output.sort()
return output
It’s O(n^2) though, with the repeated use of not in for each element of the input list.
If your input is already sorted, then there may be a simpler way to do it:
from operator import itemgetter
from itertools import groupby
unique_list = list(map(itemgetter(0), groupby(yourList)))
> but I don’t know how to retrieve the list members from the hash in alphabetical order.
Not really your main question, but for future reference Rod’s answer using sorted
can be used for traversing a dict
‘s keys in sorted order:
for key in sorted(my_dict.keys()):
print key, my_dict[key]
...
and also because tuple
‘s are ordered by the first member of the tuple, you can do the same with items
:
for key, val in sorted(my_dict.items()):
print key, val
...
For the string data
output = []
def uniq(input):
if input not in output:
output.append(input)
print output
If you want to keep order of the original list, just use OrderedDict with None
as values.
In Python2:
from collections import OrderedDict
from itertools import izip, repeat
unique_list = list(OrderedDict(izip(my_list, repeat(None))))
In Python3 it’s even simpler:
from collections import OrderedDict
from itertools import repeat
unique_list = list(OrderedDict(zip(my_list, repeat(None))))
If you don’t like iterators (zip and repeat) you can use a generator (works both in 2 & 3):
from collections import OrderedDict
unique_list = list(OrderedDict((element, None) for element in my_list))
Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:
from sets import Set
[...]
myHash = Set(myList)
but I don’t know how to retrieve the list members from the hash in alphabetical order.
I’m not married to the hash, so any way to accomplish this will work. Also, performance is not an issue, so I’d prefer a solution that is expressed in code clearly to a fast but more opaque one.
If it’s clarity you’re after, rather than speed, I think this is very clear:
def sortAndUniq(input):
output = []
for x in input:
if x not in output:
output.append(x)
output.sort()
return output
It’s O(n^2) though, with the repeated use of not in for each element of the input list.
If your input is already sorted, then there may be a simpler way to do it:
from operator import itemgetter
from itertools import groupby
unique_list = list(map(itemgetter(0), groupby(yourList)))
> but I don’t know how to retrieve the list members from the hash in alphabetical order.
Not really your main question, but for future reference Rod’s answer using sorted
can be used for traversing a dict
‘s keys in sorted order:
for key in sorted(my_dict.keys()):
print key, my_dict[key]
...
and also because tuple
‘s are ordered by the first member of the tuple, you can do the same with items
:
for key, val in sorted(my_dict.items()):
print key, val
...
For the string data
output = []
def uniq(input):
if input not in output:
output.append(input)
print output
If you want to keep order of the original list, just use OrderedDict with None
as values.
In Python2:
from collections import OrderedDict
from itertools import izip, repeat
unique_list = list(OrderedDict(izip(my_list, repeat(None))))
In Python3 it’s even simpler:
from collections import OrderedDict
from itertools import repeat
unique_list = list(OrderedDict(zip(my_list, repeat(None))))
If you don’t like iterators (zip and repeat) you can use a generator (works both in 2 & 3):
from collections import OrderedDict
unique_list = list(OrderedDict((element, None) for element in my_list))