Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?

#1
list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?

Problem: You’ve just learned about the list.clear() 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.

lst = ['Alice', 'Bob', 'Carl']

If you clear the list, it becomes empty:

lst.clear()
print(lst)
# []

However, you could have accomplished the same thing by just assigning a new empty list to the variable lst:

lst = ['Alice', 'Bob', 'Carl']
lst = []
print(lst)
# []

The output is the same. Why does the list.clear() 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:

In the second example, the variable lst_2 still points to a non-empty list object!

So, there are at least two reasons why the list.clear() method can be superior to creating a new list:

  • Release Memory: If you have a large list that fills your memory—such as a huge data set or a large file read via readlines()—and you don’t need it anymore, you can immediately release the memory with list.clear(). 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 call list.clear(), it can release the memory for other processing tasks.
  • 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 list.clear() on one variable and all other variables will see it, or you must call var1 = [], var2 = [], ..., varn = [] for all variables. This can be a pain if you have many variables.

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)!

Leaving the Rat Race with Python Book

The post list.clear() vs New List — Why Clearing a List Rather Than Creating a New One? first appeared on Finxter.



https://www.sickgaming.net/blog/2020/11/...a-new-one/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Creating Beautiful Heatmaps with Seaborn xSicKxBot 0 17 12-27-2020, 02:31 AM
Last Post: xSicKxBot
  [Tut] How to Convert an Integer List to a Float List in Python xSicKxBot 0 30 12-15-2020, 04:03 AM
Last Post: xSicKxBot
  [Tut] Python Function Call Inside List Comprehension xSicKxBot 0 45 11-16-2020, 02:42 AM
Last Post: xSicKxBot
  [Tut] How to Convert a Float List to an Integer List in Python xSicKxBot 0 49 11-11-2020, 09:35 PM
Last Post: xSicKxBot
  [Tut] How to Read a File Line-By-Line and Store Into a List? xSicKxBot 0 52 10-24-2020, 06:12 PM
Last Post: xSicKxBot
  [Tut] Python Reverse List with Slicing — An Illustrated Guide xSicKxBot 0 95 10-02-2020, 04:24 AM
Last Post: xSicKxBot
  [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 90 10-01-2020, 02:21 AM
Last Post: xSicKxBot
  [Tut] How to Get the Last Element of a Python List? xSicKxBot 0 84 09-28-2020, 02:36 AM
Last Post: xSicKxBot
  [Tut] List Changes After Assignment — How to Clone or Copy It? xSicKxBot 0 84 09-27-2020, 12:14 AM
Last Post: xSicKxBot
  [Tut] How to Sort and Return a Python List in One Line? xSicKxBot 0 77 09-09-2020, 07:08 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: Zacattack440
12-30-2020 07:30 PM
» Replies: 37
» Views: 1573
News - These Were The Best-Selling Switc...
Last Post: xSicKxBot
12-30-2020 05:23 PM
» Replies: 0
» Views: 11
(GTA V) [1.50/EV1]-Valance PC Menu v2.5-...
Last Post: testeros
12-30-2020 02:59 PM
» Replies: 1
» Views: 531
Cod Black Ops 3; Online Multi and Zombie...
Last Post: testeros
12-30-2020 02:58 PM
» Replies: 2
» Views: 1732
[Tut] Python — How to Import Modules Fro...
Last Post: xSicKxBot
12-30-2020 11:08 AM
» Replies: 0
» Views: 6
(Indie Deal) FREE 7,62 High Calibre, Out...
Last Post: xSicKxBot
12-30-2020 11:08 AM
» Replies: 0
» Views: 6
News - Level-5 Boss Teases 2021 Announce...
Last Post: xSicKxBot
12-30-2020 11:08 AM
» Replies: 0
» Views: 7
News - Feature: Nintendo Switch Ports We...
Last Post: xSicKxBot
12-30-2020 11:08 AM
» Replies: 0
» Views: 8
News - Spider-Man: Miles Morales' Dinner...
Last Post: xSicKxBot
12-30-2020 11:08 AM
» Replies: 0
» Views: 237
Xbox Wire - This Week on Xbox: December ...
Last Post: xSicKxBot
12-30-2020 01:48 AM
» Replies: 0
» Views: 14

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020