5

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)

Mitch
  • 1,604
  • 4
  • 20
  • 36

2 Answers2

3

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())
srowland
  • 1,625
  • 1
  • 12
  • 19
3

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.

Mazdak
  • 105,000
  • 18
  • 159
  • 188