Python Comprehensions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:


=Overview=
=Overview=
A comprehension is a compact way of creating a data structure from one or more iterators.  
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===
===List Comprehensions===
A list comprehension produces a list from an [[Python_Language#Iterable_Types|iterable type]] by applying an expression to each of the elements, and optionally a condition.
====Simple List Comprehension====
<syntaxhighlight lang='py'>
[<expression> for <var> in <iterable>
</syntaxhighlight>
====Conditional List Comprehension====
<syntaxhighlight lang='py'>
[<expression> for <var> in <iterable> if <condition>]
</syntaxhighlight>

Revision as of 01:34, 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>]