List Initialization
nums = [0, 1, 2]
A list named nums is created.
It contains three elements: 0, 1, and 2.
Output List Creation
out = []
An empty list out is created.
This list will store the final result.
Loop Execution
for i in range(3):
The loop runs 3 times.
Values of i in each iteration:
First → i = 0
Second → i = 1
Third → i = 2
Filter Operation
filter(lambda x: x == i, nums)
filter() checks each element of nums.
The lambda function keeps only values where x == i.
Converting Filter to List
list(filter(...))
Converts the filtered result into a list.
Example results per iteration:
i = 0 → [0]
i = 1 → [1]
i = 2 → [2]
Extending the Output List
out += ...
+= extends the list out.
It adds elements one by one, not as a nested list.
out after each iteration:
i Added out
0 [0] [0]
1 [1] [0, 1]
2 [2] [0, 1, 2]
Final Output
print(out)
Prints the final list.
Output
[0, 1, 2]

0 Comments:
Post a Comment