Various ways to clear a list in Python

Various ways to clear a list in Python

 

Various ways to clear a list in Python

In this article, I will show you how you can easily clear a list in Python it’s really simple however, there are many ways of clearing the list by using methods of different constructs allowed by Python language.

So, Let’s try to understand all of the methods one by one.

 

Method (1)By using clear() method

# Python program for clear a list 

# By using clear() method 

  

# Creating list

CLEARALIST = [8, 9, 3, 1] 

print(‘CLEARALIST before clear:’, CLEARALIST) 

  

# Cleaning list 

CLEARALIST.clear() 

print(‘CLEARALIST after clear:’, CLEARALIST) 

 

Output – 

CLEARALIST before clear: [8, 9, 3, 1]

CLEARALIST after clear: []

 

Method (2): In this method, we need to reinitializing the list

The initialization of the list in that range initializes the list with no value that is a list of size 0. 

we’ll see the example demonstrating Method 1 and 2 to clear list

# Python3 code for demonstrating 

# clearing a list applying 

# clear & reinitializing 

  

# Initializing lists 

list11 = [2, 4, 6] 

list22 = [1, 3, 5] 

  

# Printing list11 before deleting 

print (“List11 before deleting is : ” 

+ str(list11)) 

  

# deleting list using clear() 

list11.clear() 

  

# Printing list11 after clearing 

print (“List11 after clearing using clear() : ” 

+ str(list11)) 

  

# Printing list22 before deleting 

print (“List22 before deleting is : “

 + str(list22)) 

  

# deleting list using reinitialization 

list22 = [] 

  

# Printing list22 after reinitialization 

print (“List22 after clearing using reinitialization : ” 

+ str(list22)) 

 

Output –

List11 before deleting is : [2, 4, 6]

List11 after clearing using clear() : []

List22 before deleting is : [1, 3, 5]

List22 after clearing using reinitialization : []

 

Method (3): By Applying “*= 0”

This is a rarely used method, but this method eliminates all elements of the list and makes it clear.

# Python3 code for demonstrating

# clearing a list by using 

# *= 0 method 

  

# Initializing lists 

list11 = [2, 4, 6] 

  

# Printing list11 before deleting 

print (“List11 before deleting is : ” + str(list11)) 

  

# deleting list using *= 0 

list11 *= 0

  

# Printing list11 after *= 0 

print (“List11 after clearing using *= 0: ” + str(list11)) 

 

Output –

List1 before deleting is : [2, 4, 6]

List1 after clearing using *= 0: []

 

Method (4): By using del 

del can be used to clear the list elements in scope if we don’t give a range, all those elements are deleted.

# Python3 code for demonstrate 

# clearing a list by using 

# del method 

  

# Initializing lists 

list11 = [2, 4, 6] 

list22 = [1, 3, 5] 

  

# Printing list11 before deleting 

print (“List11 before deleting is : ” + str(list11)) 

  

# deleting list11 using del 

del list11[:] 

print (“List11 after clearing using del : ” + str(list11)) 

  

  

# Printing list22 before deleting 

print (“List22 before deleting is : ” + str(list22)) 

  

# deleting list using del 

del list22[:] 

print (“List22 after clearing using del : ” + str(list22)) 

 

Output –

List11 before deleting is : [2, 4, 6]

List11 after clearing using del : []

List22 before deleting is : [1, 3, 5]

List22 after clearing using del : []

 

Some more methods – 

Python List clear()

The clear() method discards all items from the list.

Syntax of clear() method is:

list.clear()

clear() Parameters

The clear() method doesn’t need any parameters.

Return Value from clear()

The clear() method only clears the given list. It doesn’t return any value.

 

Example 1: Functioning of clear() method

# Defining a list

list = [{11, 22}, (‘a’), [‘11.11’, ‘22.22’]]

# clearing the list

list.clear()

print(‘List:’, list)

If you run the program, the output must be:

List: []

Note: However, if you are using Python 2 or Python 3.2 and following, you cannot apply the clear() method. You can apply the del operator alternatively.

 

Example 2: Clearing the List Using del

# Defining a list

list = [{11, 22}, (‘a’), [‘11.11’, ‘22.22’]]

# clearing the list

del list[:]

print(‘List:’, list)

Output –

List: []

 

Related Articles :

  • How to clear all items of a C# list
  • How to clear a list in C++

 

Also, clean your bad, spam email list and increase your mail sending reputation using Clearalist – Email list cleaning service

2 Responses

Leave a Reply