close

How to clear a List in Python

Hi guys. How are you all? I hope you all are fine. Today in this tutorial we will learn about how to Clear a List in Python. By using some methods you can clear the whole list and make it empty list. So here I am giving you some methods which will help you. So lets learn about of this.

How to clear a List in Python

  1. Clear a List in Python

    to clear a List in Python just Use clear(). By using clear() function you can clear a list in python. First of all make yopur list and print it. then use the clear() function and then print the list. You can see the empty list in my output. So lets learn about of this method by given below example. Lets learn it. mylst = [25,54,85,65,12] print(mylst) mylst.clear() print(mylst) Output : [25, 54, 85, 65, 12] []

  2. How to clear a List in Python

    to clear a List in Python just Use del slice operator. Here we will use del to clear the lists. Del is used to clear the memory in python. hEre first of all i am making my list and then print it. Then i am using del function with the slice method and then print it. It will give us the empty list as you can see in my output. So lets learn about of this by given below emthods: mylst = [25,54,85,65,12] lst2 = mylst print(mylst) print(lst2) del mylst[:] print(mylst) print(lst2) Output : [25, 54, 85, 65, 12] [25, 54, 85, 65, 12] [] []

Method 1: Use clear()

By using clear() function you can clear a list in python. First of all make yopur list and print it. then use the clear() function and then print the list. You can see the empty list in my output. So lets learn about of this method by given below example. Lets learn it.

mylst = [25,54,85,65,12]
print(mylst)
mylst.clear()
print(mylst)

Output :

[25, 54, 85, 65, 12]
[]

Method 2: Use del slice operator

Here we will use del to clear the lists. Del is used to clear the memory in python. hEre first of all i am making my list and then print it. Then i am using del function with the slice method and then print it. It will give us the empty list as you can see in my output. So lets learn about of this by given below emthods:

mylst = [25,54,85,65,12]
lst2 = mylst
print(mylst)
print(lst2)

del mylst[:]
print(mylst)
print(lst2)

Output :

[25, 54, 85, 65, 12]
[25, 54, 85, 65, 12]
[]
[]

Method 3: Multiply the list with 0

You can multiply the whole list with 0 which will clear the whole list. That will clear all the previous refrences and clear the whole list. So by using this method you can easily clear the lists. So without wasting your time let’s learn about this method. I hope this will help. It will clear your list as you can see in my output. Lets learn :

mylst = [25,54,85,65,12]
lst2 = mylst
print(mylst)
print(lst2)

mylst *= 0
print(mylst)
print(lst2)

Output :

[25, 54, 85, 65, 12]
[25, 54, 85, 65, 12]
[]
[]

Conclusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment