Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cleanest way to do a sort plus uniq on a Python list?

Tags:

python

unique

Consider a Python list my_list containing ['foo', 'foo', 'bar'].

What is the most Pythonic way to uniquify and sort a list ?
(think cat my_list | sort | uniq)

This is how I currently do it and while it works I'm sure there are better ways to do it.

my_list = [] ... my_list.append("foo") my_list.append("foo") my_list.append("bar") ... my_list = set(my_list) my_list = list(my_list) my_list.sort() 
like image 862
knorv Avatar asked May 28 '10 18:05

knorv


People also ask

How do you sort unique values in a list in Python?

Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.

Which is the fastest sorting algorithm Python?

A best sorting algorithm in python The time complexity of quicksort is O(n log n) in the best case, O(n log n) in the average case, and O(n^2) in the worst case. Quicksort is also considered as the ” fastest” sorting algorithm because it has the best performance in the average case for most inputs.

How do you sort a list from least to greatest in Python?

Summary. Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.

How efficient is Python list sort?

The previous investigations showed us, that list. sort is slightly faster than sorted and consumes around 24% less memory. However, keep in mind that list. sort is only implemented for lists, whereas sorted accepts any iterable.


2 Answers

my_list = sorted(set(my_list)) 
like image 135
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 20:10

Ignacio Vazquez-Abrams


# Python ≥ 2.4 # because of (generator expression) and itertools.groupby, sorted  import itertools  def sort_uniq(sequence):     return (x[0] for x in itertools.groupby(sorted(sequence))) 

Faster:

import itertools, operator import sys  if sys.hexversion < 0x03000000:     mapper= itertools.imap # 2.4 ≤ Python < 3 else:     mapper= map # Python ≥ 3  def sort_uniq(sequence):     return mapper(         operator.itemgetter(0),         itertools.groupby(sorted(sequence))) 

Both versions return an generator, so you might want to supply the result to the list type:

sequence= list(sort_uniq(sequence)) 

Note that this will work with non-hashable items too:

>>> list(sort_uniq([[0],[1],[0]])) [[0], [1]] 
like image 25
tzot Avatar answered Oct 18 '22 21:10

tzot