Python Comprehensions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 16: Line 16:
====Simple List Comprehension====
====Simple List Comprehension====
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
[<expression> for <var> in <iterable>
[<expression> for <var> in <iterable>]
</syntaxhighlight>
</syntaxhighlight>
====Conditional List Comprehension====
====Conditional List Comprehension====
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
[<expression> for <var> in <iterable> if <condition>]
[<expression> for <var> in <iterable> if <condition>]
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:35, 9 July 2022

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>]