Back to Lesson

Comprehending Functions

Prevent duplicated processing cycles utilizing inline assignment limits mapping list structures.

Instructions

  • Given elements dataset
    records = [2, 4, -1, 8, 0]
    .
  • Imagine calling
    computation = (x * 2) + 1
    happens frequently.
  • Inside a list comprehension: extract only values where the calculated computation evaluates natively to > 0. Store and retain ONLY this output computed metric result.
  • Normally this requires calling computation explicitly twice! But leveraging
    walrus
    , write
    [processed for x in records if (processed := (x * 2) + 1) > 0]
    . Print the return sequence.
main.py
main.py