Python usage notes - iterable stuff

From Helpful
Jump to: navigation, search
Various things have their own pages, see Category:Python. Some of the pages that collect various practical notes include:


This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)
for a sorted copy
for in-place


The key parameter should be a callable that fetches the value to sort on from an object. This allows:

  • normalization, e.g. case-insensitive sort via:
key=lambda x: x.lower()
  • sorting on multiple aspects of things, when you return a tuple, for example:
Empty strings last too via:
key=lambda x: (x=='', x.lower())

When you're fetching from lists, dicts, or attibutes, consider that operator.attrgetter and operator.itemgetter can go a long way, e.g. allowing:


Iteratable-related tools

See also: