Idiomatic and single line? Not.
Here's a non-idiomatic butt-ugly single-line layer.
>>> x = [4, 3, 3, 2, 4, 1] >>> [y for y in (locals().__setitem__('d',{}) or x.sort() or x) if y not in d and (d.__setitem__(y, None) or True)] [1, 2, 3, 4]
If a simple two-line line is acceptable:
x = [4, 3, 3, 2, 4, 1] x = dict(map(None,x,[])).keys() x.sort()
Or create two small helper functions (works for any sequence):
def unique(it): return dict(map(None,it,[])).keys() def sorted(it): alist = [item for item in it] alist.sort() return alist print sorted(unique([4, 3, 3, 2, 4, 1]))
gives
[1, 2, 3, 4]
And finally, a semi-pyphonic one liner:
x = [4, 3, 3, 2, 4, 1] x.sort() or [s for s, t in zip(x, x[1:] + [None]) if s != t]