Python Book
πŸ‡ΊπŸ‡¦ Stand with UkraineπŸŽ“Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page
  • List comprehension
  • Dictionary comprehension
  • Set comprehension
  • Generator expression

Was this helpful?

Edit on Git
  1. 5. Functional Programming

Comprehensions

Comprehensions are considered as most "Pythonic" way of constructing needed data on-the-fly.

  • List comprehension

  • Dictionary comprehension

  • Set comprehensions

  • Generator expression*

List comprehension

Bread and butter of day-to-day Python programming

πŸͺ„ Code:

[x for x in range(0, 10)]

πŸ“Ÿ Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

πŸͺ„ Code:

[x for x in range(0,10) if x%2 == 0]

πŸ“Ÿ Output:

[0, 2, 4, 6, 8]

πŸͺ„ Code:

[(x, y) for x in range(0,10) if x%2 == 0 for y in range(x) if y%2 != 0]

πŸ“Ÿ Output:

[(2, 1),
 (4, 1),
 (4, 3),
 (6, 1),
 (6, 3),
 (6, 5),
 (8, 1),
 (8, 3),
 (8, 5),
 (8, 7)]

Dictionary comprehension

Useful to create a dictionary with the same (default) value or predefined by some logic

πŸͺ„ Code:

{x: str(x) for x in range(5)}

πŸ“Ÿ Output:

{0: '0', 1: '1', 2: '2', 3: '3', 4: '4'}

πŸͺ„ Code:

{x: y for x in range(3) for y in range(3)}

πŸ“Ÿ Output:

{0: 2, 1: 2, 2: 2}

Set comprehension

Not so widely used but still can be quite helpful. For example if you read lines from the file you can collect unqiue ones.

πŸͺ„ Code:

list_with_duplicated = [1, 1, 2, 3, 2, 1, 4, 2]
print({x for x in list_with_duplicated})
print(set(list_with_duplicated)) # recommended way

print({x for x in list_with_duplicated if x % 2}) # more logical usage

πŸ“Ÿ Output:

{1, 2, 3, 4}
{1, 2, 3, 4}
{1, 3}

Generator expression

"Kind of" comprehension but instead of returning sequence as other do, generator expression returns generator object.

πŸͺ„ Code:

(x * x for x in range(10))

πŸ“Ÿ Output:

<generator object <genexpr> at 0x0000023DF7253678>

πŸͺ„ Code:

for x in (x * x for x in range(10)):
    print(x, end=" ")
0 1 4 9 16 25 36 49 64 81
PreviousFunctoolsNextAdditional modules

Last updated 2 years ago

Was this helpful?