Python Comprehensions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

TODO

  • PROCESS IPy Comprehensions Page 84.
  • PROCESS PyOOP "Comprehensions" + "List comprehensions" + "Set and dictionary comprehensions"

Overview

A comprehension is a compact way of creating a data structure from one or more iterators. They are essentially loops with a more compact syntax.

List Comprehensions

A list comprehension produces a list from an iterable type by applying an expression to each of the elements, and optionally a condition.

Simple List Comprehension

[<expression> for <var> in <iterable>

Conditional List Comprehension

[<expression> for <var> in <iterable> if <condition>]