{"id":3020,"date":"2023-10-20T08:32:59","date_gmt":"2023-10-20T03:02:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=3020"},"modified":"2023-11-10T11:51:09","modified_gmt":"2023-11-10T06:21:09","slug":"python-how-to-remove-multiple-elements-from-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-remove-multiple-elements-from-list\/","title":{"rendered":"Python : How to Remove Multiple Elements from List ?"},"content":{"rendered":"
In Python, a list is used to store the sequence of different types of data. Python lists are mutable, which means we can change their elements after they’ve been formed. However, Python has six data types that can be used to store sequences, with the list being the most common and accurate.<\/p>\n
A list can be described as a set of different types of values or objects. The comma (,) separates the things in the list, which are enclosed in square brackets [].<\/p>\n
The following is a list of possible python methods to remove multiple elements from the list:<\/p>\n
The remove() method removes the first matching element from the list (which is passed as an argument).<\/p>\n
remove() parameters:<\/strong><\/p>\n Let us remove all elements which are divisible by 2.<\/p>\n Traverse the list and remove the element if it is divisible by 2.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Another way to delete an element from a list is to use its index in the del statement. It differs from the pop() function in that it does not return the removed element. Unlike the slicing feature, this does not generate a new list.<\/p>\n If we want to remove multiple elements from a list based on an index range, we can use the del keyword.<\/p>\n Suppose we want to delete elements from 2 to 5 indices we can use slicing and del keyword to do that.<\/p>\n It will remove the elements from index1 to index2 \u2013 1 from the list.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Python’s syntax for deriving one list from another is concise. These expressions are known as list comprehensions. Python list comprehensions make it extremely simple to apply a function or filter to a list of items. List comprehensions can be very useful when used correctly, but they can also be very unreadable if used incorrectly.<\/p>\n Let us remove all elements which are divisible by 2.<\/p>\n We can achieve that easily by list comprehension as given below:<\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In Python, a list is used to store the sequence of different types of data. Python lists are mutable, which means we can change their elements after they’ve been formed. However, Python has six data types that can be used to store sequences, with the list being the most common and accurate. A list can …<\/p>\n\n
# function which remove multiple items from the list\r\ndef removeMultiple(givenlist):\r\n # Traverse the list\r\n for element in list(givenlist):\r\n # Checking the given condition\r\n if(element % 2 == 0):\r\n # remove the element from the given list\r\n givenlist.remove(element)\r\n # return the given list\r\n return(givenlist)\r\n\r\n\r\n# Driver code\r\n# Given list\r\ngivenlist = [1, 2, 3, 4, 5, 6, 7, 8]\r\n# passing the list to removeMultiple function\r\nprint(removeMultiple(givenlist))\r\n<\/pre>\n
[1, 3, 5, 7]<\/pre>\n
Method #2:Using del keyword and slicing<\/h3>\n
# function which remove multiple items from the list\r\ndef removeMultiple(givenlist):\r\n # using del keyword and removing indices from 2 to 5\r\n # increment the right index with 1 to remove it\r\n del givenlist[2:6]\r\n # return the given list\r\n return(givenlist)\r\n\r\n\r\n# Driver code\r\n# Given list\r\ngivenlist = [1, 2, 3, 4, 5, 6, 7, 8]\r\n# passing the list to removeMultiple function\r\nprint(removeMultiple(givenlist))\r\n<\/pre>\n
[1, 2, 7, 8]<\/pre>\n
Method #3: Using List Comprehension<\/h3>\n
# function which remove multiple items from the list\r\ndef removeMultiple(givenlist):\r\n # using list comprehension\r\n givenlist = [element for element in givenlist if element % 2 != 0]\r\n # return the given list\r\n return(givenlist)\r\n\r\n\r\n# Driver code\r\n# Given list\r\ngivenlist = [1, 2, 3, 4, 5, 6, 7, 8]\r\n# passing the list to removeMultiple function\r\nprint(removeMultiple(givenlist))\r\n<\/pre>\n
[1, 3, 5, 7]<\/pre>\n
\n