Does sort remove duplicates in python?

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.

asked Jan 26, 2009 at 14:09

Josh GloverJosh Glover

24k25 gold badges86 silver badges128 bronze badges

3

A list can be sorted and deduplicated using built-in functions:

myList = sorted[set[myList]]
  • set is a built-in function for Python >= 2.3
  • sorted is a built-in function for Python >= 2.4

Bengt

13.5k6 gold badges47 silver badges65 bronze badges

answered Jan 26, 2009 at 14:16

8

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

answered Jan 26, 2009 at 14:48

3

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

answered May 10, 2016 at 9:49

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.

answered Jan 26, 2009 at 14:16

unwindunwind

383k64 gold badges462 silver badges594 bronze badges

0

> 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
    ...

answered Jan 26, 2009 at 15:22

davidavrdavidavr

14.1k4 gold badges27 silver badges31 bronze badges

For the string data

 output = []

     def uniq[input]:
         if input not in output:
            output.append[input]
 print output     

answered Jun 26, 2013 at 9:36

Does sort function remove duplicates?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.

How do you sort and remove duplicates from a list in Python?

5 Ways to Remove Duplicates from a List in Python.
Method 1: Naïve Method..
Method 2: Using a list comprehensive..
Method 3: Using set[].
Method 4: Using list comprehensive + enumerate[].
Method 5: Using collections. OrderedDict. fromkeys[].

How do you remove duplicates from Python?

You can remove duplicates from a Python using the dict. fromkeys[], which generates a dictionary that removes any duplicate values. You can also convert a list to a set. You must convert the dictionary or set back into a list to see a list whose duplicates have been removed.

Does list Take duplicates in Python?

Python list can contain duplicate elements.

Chủ Đề