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] [Floyd’s Algorithm] How to Detect a Cycle in a Linked List in Python? xSicKxBot 0 9 05-03-2021, 03:55 PM
Last Post: xSicKxBot
  [Tut] Sorting a List Based on Values From Another List xSicKxBot 0 16 04-29-2021, 02:42 PM
Last Post: xSicKxBot
  [Tut] How To Apply A Function To Each Element Of A List? xSicKxBot 0 23 04-25-2021, 07:02 PM
Last Post: xSicKxBot
  [Tut] How to Convert a String List to an Integer List in Python xSicKxBot 0 36 04-09-2021, 01:50 PM
Last Post: xSicKxBot
  [Tut] How to Convert a Float List to a String List in Python xSicKxBot 0 34 04-07-2021, 02:53 PM
Last Post: xSicKxBot
  [Tut] Check If All Elements In A List Are Identical xSicKxBot 0 44 03-20-2021, 10:12 AM
Last Post: xSicKxBot
  [Tut] How to Loop Through a Python List in Pairs, Sliding Windows, and Batches? xSicKxBot 0 45 03-17-2021, 03:08 PM
Last Post: xSicKxBot
  [Tut] How to Create a Tuple from a String and a List of Strings xSicKxBot 0 49 03-13-2021, 09:55 AM
Last Post: xSicKxBot
  [Tut] How to Replace One or More List Elements at Specific Indices in Python? xSicKxBot 0 52 03-09-2021, 05:03 PM
Last Post: xSicKxBot
  [Tut] PyPubSub – Creating Your First Publish Subscribe App in Python xSicKxBot 0 68 02-11-2021, 06:37 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) FREE Disgraced, Waifu Desir...
Last Post: xSicKxBot
Today 07:52 PM
» Replies: 0
» Views: 3
Unreal Engine May 2021 Free Marketplace ...
Last Post: xSicKxBot
Today 07:52 PM
» Replies: 0
» Views: 4
Microsoft - Humans and AI: Meet Juan Lav...
Last Post: xSicKxBot
Today 07:51 PM
» Replies: 0
» Views: 4
Fedora - Introducing the Fedora i3 Spin
Last Post: xSicKxBot
Today 07:51 PM
» Replies: 0
» Views: 3
News - Detective Pikachu Star Doesn’t Se...
Last Post: xSicKxBot
Today 07:51 PM
» Replies: 0
» Views: 3
News - NPD Reveals Top 10 Best-Selling S...
Last Post: xSicKxBot
Today 07:51 PM
» Replies: 0
» Views: 3
News - Blizzard Loses Millions Of Monthl...
Last Post: xSicKxBot
Today 07:50 PM
» Replies: 0
» Views: 13
AppleInsider - Apple Pay launches in Isr...
Last Post: xSicKxBot
Today 12:18 PM
» Replies: 0
» Views: 10
Microsoft - Teachers: Thank you for insp...
Last Post: xSicKxBot
Today 12:18 PM
» Replies: 0
» Views: 6
News - UK Charts: New Pokémon Snap Tops ...
Last Post: xSicKxBot
Today 12:17 PM
» Replies: 0
» Views: 7

[-]
Twitter

Copyright © SickGaming.net 2012-2020