TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python List clear() method with Examples

Python List clear() method with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python List clear() Method

Python clear() method removes all the elements from the list. It clear the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty. The syntax and examples are described below.

Signature

clear()

Parameters

No parameter

Return

It returns None.

Let's see some examples of clear() method to understand it's functionality.

Python List clear() Method Example 1

Let's see a simple example in which clear() method is used to clear a list. The clear() method clears all the elements from the list.

# Python list clear() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list.clear()
print("After clearing:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After clearing:

Python List clear() Method Example 2

If the list is already empty, the method returns nothing. See the example below.

# Python list clear() Method
# Creating a list
list = []
for l in list:  # Iterating list
    print(l)
list.clear()
print("After clearing:")
for l in list:  # Iterating list
    print(l)

Output:

After clearing:

Next TopicPython Lists




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf