11-30-2020, 06:53 PM
list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?
Problem: You’ve just learned about the method in Python. You wonder, what’s its purpose? Why not creating a new list and overwriting the variable instead of clearing an existing list?
Example: Say, you have the following list.
If you clear the list, it becomes empty:
However, you could have accomplished the same thing by just assigning a new empty list to the variable:
The output is the same. Why does the method exist in the first place?
If you go through the following interactive memory visualizer, you’ll see that both variants lead to different results if you have multiple variables pointing to the list object:
https://pythontutor.com/iframe-embed.htm...nces=false
In the second example, the variable still points to a non-empty list object!
So, there are at least two reasons why the method can be superior to creating a new list:
Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!
![[Image: final_cover-200x300.jpg]](https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-200x300.jpg)
The post list.clear() vs New List — Why Clearing a List Rather Than Creating a New One? first appeared on Finxter.
Problem: You’ve just learned about the
Code:
list.clear()Example: Say, you have the following list.
Code:
lst = ['Alice', 'Bob', 'Carl']If you clear the list, it becomes empty:
Code:
lst.clear()
print(lst)
# []However, you could have accomplished the same thing by just assigning a new empty list to the variable
Code:
lstCode:
lst = ['Alice', 'Bob', 'Carl']
lst = []
print(lst)
# []The output is the same. Why does the
Code:
list.clear()If you go through the following interactive memory visualizer, you’ll see that both variants lead to different results if you have multiple variables pointing to the list object:
https://pythontutor.com/iframe-embed.htm...nces=false
In the second example, the variable
Code:
lst_2So, there are at least two reasons why the
Code:
list.clear()- Release Memory: If you have a large list that fills your memory—such as a huge data set or a large file read via —and you don’t need it anymore, you can immediately release the memory withCode:
readlines(). Especially in interactive mode, Python doesn’t know which variable you still need – so it must keep all variables till session end. But if you callCode:list.clear(), it can release the memory for other processing tasks.Code:list.clear()
- Clear Multiple List Variables: Multiple variables may refer to the same list object. If you want to reflect that the list is now empty, you can either call on one variable and all other variables will see it, or you must callCode:
list.clear()for all variables. This can be a pain if you have many variables.Code:var1 = [], var2 = [], ..., varn = []
Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!
![[Image: final_cover-200x300.jpg]](https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-200x300.jpg)
The post list.clear() vs New List — Why Clearing a List Rather Than Creating a New One? first appeared on Finxter.

