Explanation:
๐น Step 1: Create List
x = [3,2,1]
A list is created:
[3,2,1]
๐น Step 2: Create Special Iterator
it = iter(x.pop, 2)
⚠️ This is a VERY special form of iter() ๐
Syntax:
iter(callable, sentinel)
๐น Step 3: Understand x.pop
x.pop
This is NOT calling function yet ❌
It is just giving function reference.
So iterator will repeatedly do:
x.pop()
again and again.
๐น Step 4: Understand Sentinel Value
2
This is sentinel value.
Iterator stops WHEN:
x.pop() == 2
๐น Step 5: Execute next(it)
next(it)
Iterator internally calls:
x.pop()
⚠️ pop() removes LAST element.
Current list:
[3,2,1]
So:
x.pop() → 1
List becomes:
[3,2]
๐น Step 6: Compare with Sentinel
Returned value:
1
Sentinel:
2
Since:
1 != 2
iterator continues normally.
So:
next(it) → 1
๐น Step 7: Print Result
print(1)
๐ Final Output:
1
๐ฅ Final Output
1

0 Comments:
Post a Comment