Different ways to clear a list in PythonThere are many ways of clearing the list through methods of different constructs offered by Python language. Let’s try to understand each of the method one by one. Show
Article Tags :
Python
Python list-programs python-list python-list-functions Practice Tags :
python-list Python – Remove empty List from ListSometimes, while working with python data, we can have a problem in which we need to filter out certain empty data. These can be None, empty string etc. This can have application in many domains. Let’s discuss certain ways in which removal of empty lists can be performed. Method #1 : Using list comprehension
Output :
The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]
Method #2 : Using filter()
Output :
The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]
Article Tags :
Python Python Programs
Python list-programs Python List clear()In this tutorial, we will learn about the Python List clear() method with the help of examples. The clear() method removes all items from the list. Exampleprime_numbers = [2, 3, 5, 7, 9, 11] # remove all elements prime_numbers.clear()# Updated prime_numbers List
print('List after clear():', prime_numbers)
# Output: List after clear(): []Different ways to clear a list in PythonPythonServer Side ProgrammingProgramming Clearing up all the elements in a python list can be done in many ways. Below are the some of the methods which are implemented to achieve this. Python remove() methodPython removes () method is a built-in method available with the list. It helps to remove the given very first element matching from the list. Syntax:list.remove(element)The element that you want to remove from the list. ReturnValue There is no return value for this method. Tips for using remove() method:Following are the important points to remember when using remove () method:
Example: Using remove() method to remove an element from the listHere is a sample list that i have my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya. Output: ['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback (most recent calllast): File "display.py", line 9, in <module> my_list.remove(100) ValueError: list.remove(x): x not in the listMethods:Many methods exist in Python to modify the list. Some common methods to add and remove data in the list are mentioned here. insert (index,item): This method is used to insert any item in the particular index of the list and right shift the list items. append (item): This method is used to add new element at the end of the list. extend (anotherList): The items of one list can be inserted at the end of another list by using this method. remove (item): This method is used to remove particular item from the list. pop (index): The method is used to remove item from the list based on index value. del(): This method is used to remove the particular item of the list or slice the list. clear(): This method is used to remove all items of a list Add items into the list:Different ways to add items in Python list are shown in this part of the tutorial. Example 1: Insert item using insert() methodCreate a python file with the following script to see the use of insert() method. A new item will be inserted in the third position of the list and the other items will be shifted right after running the script. # Declare list listdata = [89, 56, 90, 34, 89, 12] # Insert data in the 2nd position listdata.insert(2, 23) # Displaying list after inserting print("The list elements are") for i in range(0, len(listdata)): print(listdata[i]) Output: The following output will appear after running the script. Example 2: Insert item using append() methodCreate a python file with the following script to see the use of append() method. It is mentioned before that append() method inserts data at the end of the list. So, ‘Toshiba’ will be inserted at the end of listdata after running the script. # Define the list listdata = ["Dell", "HP", "Leveno", "Asus"] # Insert data using append method listdata.append("Toshiba") # Display the list after insert print("The list elements are") for i in range(0, len(listdata)): print(listdata[i]) Output: The following output will appear after running the script. Example 3: Insert item using extend() methodCreate a python file with the following script to see the use of extend() method. Here, two lists are declared in the script which are combined together by using extend() method. The items of the second list will be added at the end of the first list. # initializing the first list list1 = ['html', 'CSS', 'JavaScript', 'JQuery'] # initializing the second list list2 = ['PHP', 'Laravel', 'CodeIgniter'] # Combine both lists using extend() method list1.extend(list2) # Display the list after combing print ("The list elements are :") for i in range(0, len(list1)): print(list1[i]) Output: The following output will appear after running the script. Python remove empty elements from a list ExampleSimple examples code. Using list comprehensionSimp;e iterate through the list and add the non-empty elements. list1 = ['A', ' ', ' ', 'B', ' ', 'C'] res = [ele for ele in list1 if ele.strip()] print(res) list2 = [1, 6, [], 3, [], [], 9] res = [ele for ele in list2 if ele != []] print(res)Output: 1. Using list.clear() functionlist.clear() is the recommended solution in Python 3 to remove all items from the list.
DownloadRun Code |