Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten, remove duplicates, and sort a list of lists in python

From this answer I have a flattened list.

Now I want to remove duplicates and sort the list. Currently I have the following:

x = itertools.chain.from_iterable(results[env].values()) #From the linked answer
y = sorted(list(set(x)), key=lambda s:s.lower())

Is there a better way of accomplishing this? In my case x is of size ~32,000 and y ends up being of size ~1,100. What I have works, but I'd like to see if there's anything better (faster, more readable, etc)

like image 890
Mitch Avatar asked Apr 12 '16 19:04

Mitch


2 Answers

Actually, if you just remove the list() which isn't needed, you've got a nice neat solution to your original problem. Your code is perfectly readable and efficient I think.

y = sorted(set(x), key=lambda s:s.lower())
like image 115
srowland Avatar answered Oct 31 '22 11:10

srowland


Since results[env] is a dictionary you can create a set of union values instead of flattening the values then sort the result:

>>> sorted(set().union(*results[env].values()), key=str.lower)

Also note that you don't need a lambda function as your key, you can simple use str.lower method.

like image 32
Mazdak Avatar answered Oct 31 '22 12:10

Mazdak