python-list might be a good place to ask, but a quick pointer is to not create so many lists if you can help it. Assuming this is Python 3 then you have `XValues`

, `sorted()`

, and the final list that you do append to `SortedList`

all duplicating data; you’re duplicating all your data 3 times per loop. But if you make `XValues`

be a generator expression and filter using either `filter()`

or a genexp then you only need to end up with a concrete list if you call `sorted()`

last.

I’m also not sure why you’re using `&`

instead of `and`

; if you want the index using a boolean then either just use the boolean directly or call `int()`

on it.

And can `y`

be anything but a float? If not then you don’t need `y != 0.`

and you can express it as just `y`

.

```
import operator
SortedList = []
for j in range(Size):
XValues = (DF[DF['Label'] == i and DF['X'] == j]['Y'].mean() for i in Labels)
filtered_list = filter(operator.itemgetter[0], zip(XValues, Labels))
SortedList.append([x for y, x in sorted(filtered_list)])
```