Comprehensions and Sorting

Comprehensions and Sorting: Comprehensions: New Lists in One Line, Sorting with key and lambda.

Updated

  1. Comprehensions: New Lists in One Line (Apprentice): A comprehension builds a new list, dictionary or set from another collection in one line, written as what goes in, for each item, if a condition holds, and replaces the empty-list-and-append loop whenever the whole job is building one collection.
  2. Sorting with key and lambda (Journeyman): sorted() takes a key, a function it calls on each item to get the value to compare, so a short lambda can sort recipes by their minutes or a dictionary's entries by their values, from largest to smallest with reverse=True.