Assuming that 1 -
is a four-element list is there any difference between these two statements?
Assuming that 1 -
is a four-element list is there any difference between these two statements?
The first line using 'del lst' deletes the entire list object, thus making any subsequent reference to 'lst' result in a NameError. The second line using 'del lst[:]' empties the list by deleting all its elements, but the list object itself remains, so 'lst' references an empty list.
Some of these questions are so confusing to read at a first. Here is an example to show what the hell they are talking about first = [1,2,3,4] print(first) del first[:] print(first) del first print(first) output: [1, 2, 3, 4] [] Traceback (most recent call last): File "main.py", line 6, in <module> print(first) NameError: name 'first' is not defined answer is B
B. yes, there is, the first line deletes the list as a whole, the second line just empties the list
Ans : B . del l and subsequent use of variable l will return NameError and and del l1[:] will return empty list