Step 1: Initial List
clcoding = [1, 2, 3]List length = 3
Step 2: Understanding the Lambda
f = lambda x: (clcoding.append(0), len(clcoding))[1]This line does two things at once using a tuple:
| Part | What it Does |
|---|---|
| clcoding.append(0) | Adds 0 to the list |
| len(clcoding) | Gets updated length |
| [1] | Returns second value only |
✅ So each time f(x) runs → list grows by 1 → new length is returned
Step 3: map() is Lazy
m = map(f, clcoding) map() does NOT run immediately.
It runs only when next(m) is called.
Step 4: Execution Loop (3 Times)
▶ First next(m)
-
List before: [1, 2, 3]
append(0) → [1, 2, 3, 0]
len() → 4
-
✅ Prints: 4
▶ Second next(m)
-
List before: [1, 2, 3, 0]
append(0) → [1, 2, 3, 0, 0]
len() → 5
-
✅ Prints: 5
▶ Third next(m)
-
List before: [1, 2, 3, 0, 0]
append(0) → [1, 2, 3, 0, 0, 0]
len() → 6
-
✅ Prints: 6
Final Output
Key Concepts Used (Interview Important)
-
✅ map() is lazy
-
✅ Mutable list modified during iteration
-
✅ Tuple execution trick inside lambda
-
✅ Side-effects inside functional calls


0 Comments:
Post a Comment