Sorting A List Comprehension In One Statement

The method list.sort() is sorting the list in place, and as all mutating methods it returns None. Use the built-in function sorted() to return a new sorted list.

result = sorted((trans for trans in my_list if trans.type in types),
                key=lambda x: x.code)

Instead of lambda x: x.code, you could also use the slightly faster operator.attrgetter("code").

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)