Let’s break this down line by line ๐
lst = [1, 2, 3]Here, you create a list named lst with three elements: 1, 2, and 3.
So right now:
lst = [1, 2, 3]Length = 3
lst.append([4, 5])
Now you use append(). Important point:
๐ append() adds the ENTIRE item as a single element
You are NOT adding 4 and 5 separately.
You are adding a list [4, 5] as one single element.
So after append:
lst = [1, 2, 3, [4, 5]]Now the list contains 4 elements:
-
1
- 2
- 3
- [4, 5] → (this is one nested list, counted as ONE element)
print(len(lst))
len(lst) counts total elements in the outer list.
So the output is:
4Common Confusion (Important!)
If you had used extend() instead of append(), the result would be different:
Now:
lst = [1, 2, 3, 4, 5]
Output would be 5


0 Comments:
Post a Comment