Various ways to clear a list in Python

Python Methods and Functions

There are many ways to clean up a list using the various constructs offered by the Python language. Let`s try to understand each of the methods one by one.

  • Method # 1: Using the clear()

    method

    # Python program to clear the list
    # using the clear () method

     
    # Create a list

    GEEK = [ 6 , 0 , 4 , 1 ]

    print ( `GEEK before clear:` , GEEK) 

      
    # Clearing list
    GEEK.clear () 

    print ( `GEEK after clear:` , GEEK) 

    Output:

     GEEK before clear: [6, 0, 4, 1] GEEK after clear: [] 

  • Method # 2: Reinitialize the list: Initializing the list in this scope will initialize the list with no value. those. a list of size 0. Let`s see an example demonstrating methods 1 and 2 to clear a list

    # Python3 demo code
    # clear the list with
    # clear and reinitialize

      
    # Initializing lists

    list1 = [ 1 , 2 , 3 ]

    list2 = [ 5 , 6 , 7 ]

      
    # Print list1 before deleting

    print ( " List1 before deleting is: " 

    +   str (list1))

     
    # deleting the list using clear ()
    list1.clear ()

     
    # Print list1 after cleaning

    print ( "List1 after clearing using clear ():"  

    + str (list1))

      
    # Print list2 before deleting

    print ( " List2 before deleting is: "

      +   str (list2))

     
    # deleting the list by reinitializing

    list2 = []

     
    # Print list2 after reinitialization

    print ( " List2 after clearing using reinitialization: " 

    + str (list2))

    Exit: < / strong>

     List1 before deleting is: [1, 2, 3] List1 after clearing using clear (): [] List2 before deleting is: [5, 6, 7] List2 after clearing using reinitialization: [] 

  • Method # 3: Using "* = 0" : This is a lesser known method, but this method removes all elements of the list and makes it empty.

    # Python3 demo code
    # clear the list with
    # * = 0 method

     
    # Initializing lists

    list1 = [ 1 , 2 , 3 ]

      
    # Print list1 before deleting

    print ( "List1 before deleting is:" + str (list1))

     
    # delete the list with * = 0

    list1 * = 0

     
    # Print list1 after * = 0

    print ( "List1 after clearing using * = 0:" + str (list1))

      Exit :

     List1 before deleting is: [1, 2, 3] List1 after clearing using * = 0: [] 

  • Method # 4: Using del : del can be used to clear list items in a range, if we don`t give a range, all items will be removed.

    # Python3 demo code
    # clear the list with
    # del method

     
    # Initializing lists

    list1 = [ 1 , 2 , 3 ]

    list2 = [ 5 , 6 , 7 ]

     
    # Print list1 before deleting

    print ( "List1 before deleting is:" + str (list1))

     
    # deleting list1 with del

    del list1 [:]

    print ( " List1 after clearing using del: " + str (list1))

     

     
    # Print list2 before deleting

    print ( "List2 before deleting is:" + str (list2))

     
    # deleting the list with del

    del list2 [:]

    print ( "List2 after clearing using del:" + str (list2))

    Exit :

     List1 before deleting is: [1, 2, 3] List1 after clearing using del: [] List2 before deleting is: [ 5, 6, 7] List2 after clearing using del: [] 




Get Solution for free from DataCamp guru