Home » Python

Python List clear() Method with Example

Python List clear() Method: Here, we are going to learn how to remove all elements from the list in Python?
Submitted by IncludeHelp, on December 03, 2019

List clear() Method

clear() method is used to clear a list i.e. it is used to remove all elements from the list.

Syntax:

    list_name.clear()

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is <class 'NoneType'>, it returns nothing.

Example 1:

# declaring a list
cities = ["New Delhi", "Mumbai", "Banglore"]

# printing elements before clearing the list
print("Before clear() method...")
print("cities = ", cities)

# clearing the list
cities.clear()

# printing elements after clearing the list
print("After clear() method...")
print("cities = ", cities)

Output

Before clear() method...
cities =  ['New Delhi', 'Mumbai', 'Banglore']
After clear() method...
cities =  []

Example 2:

# declaring the lists
x = ["ABC", "XYZ", "PQR"]
y = ["PQR", "MNO", "YXZ"]
z = ["123", "456", "789"]

# printing sets before clearing the list
print("Before clear() method...")
print("x: ", x)
print("y: ", y)
print("z: ", z)

# clearing the lists
x.clear()
y.clear()
z.clear()

# printing sets after clearing the list
print("After clear() method...")
print("x: ", x)
print("y: ", y)
print("z: ", z)

Output

Before clear() method...
x:  ['ABC', 'XYZ', 'PQR']
y:  ['PQR', 'MNO', 'YXZ']
z:  ['123', '456', '789']
After clear() method...
x:  []
y:  []
z:  []






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.