Python List clear()

In this tutorial, we will see about Python List‘s clear method.Python List clear method is used to remove all the items from the list.

Python List clear example

You can simply use clear method to delete all the items from the list.Python extend does not return anything but it actually updates the original list.
Let’s understand this with the help of simple example.

Output:

List before clear: [1, 2, 3, 4] List after clear: []

If you are using python 3.2 or below, you won’t find clear method in the list.You need to use del operator in that case.

Output:

List before clear: [1, 2, 3, 4, 5] List after clear: []

That’s all about Python List clear method.

Related Posts

  • Initialize list with zeros in Python
    25 October

    Four Ways To Initialize List With Zeros In Python

    Table of ContentsWhy Initialize List with Zeros in Python?How Can We Initialize List With Zeros In Python?Initialize List With Zeros Using * Operator In PythonInitialize List With Zeros Using  itertools.repeat() FunctionInitialize List With Zeros Using  Generator Comprehension In PythonInitialize List With Zeros Using  List Comprehension In PythonConclusion Lists in Python are one of the most […]

  • 06 October

    How to find index of all occurrences in list in Python

    Table of ContentsThe easiest method – Using list count() methodThe Basic Method – Using for loopsThe shortest method – Using the enumerate() functionThe fastest method – Using a numpy arrayUsing itertools moduleUsing more-itertools LibraryUsing index() function List is a data type in Python. It is used to store multiple values in one variable. If you […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Sort list alphabetically in Python
    31 May

    Sort list alphabetically in Python

    Table of ContentsUse of the sort() function to sort list alphabetically in Python.Use of the sorted() function sort list alphabetically in Python.Use of the QuickSort algorithm sort list alphabetically in Python. 💡 Outline You can use sort() method of list to sort list alphabetically in Python [crayon-6179796dcba9e760712435/] In this tutorial, we will discuss different ways […]

  • Find common elements in two lists Python
    25 May

    Find common elements in two lists in python

    Table of ContentsUsing the intersection() FunctionUsing List ComprehensionUsing & Operator 💡 Outline You can first convert first list into set using set() and call intersection() by passing second list as parameter to find common element in two lists in Python. [crayon-6179796dcbe98679170661/] In this tutorial, we will see different methods to find common elements in two […]

  • 11 April

    Python Remove Newline from List

    Table of ContentsIntroduction✨ Method 1: Using the replace() method✨ Method 2: Using re.sub()✨ Method 3: Using strip()✨ Method 4: Slice the List Elements✨ Method 5: Using List Comprehension and Splitlines Function✨ Method 6: Using str.maketrans(x,y) and str.translate(map)Conclusion 🎯 Objective: In this article, we will discuss the methods to remove ‘\n’ or newline characters from a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.