{"id":3207,"date":"2023-10-20T12:44:33","date_gmt":"2023-10-20T07:14:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=3207"},"modified":"2023-11-10T11:50:14","modified_gmt":"2023-11-10T06:20:14","slug":"python-remove-elements-from-list-by-value","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-remove-elements-from-list-by-value\/","title":{"rendered":"Python: Remove Elements from List by Value"},"content":{"rendered":"
Lists are ordered sequences that can contain a wide range of object types. Members on lists can also be duplicated. Lists in Python are equivalent to arrays in other programming languages. There is, however, one significant difference. Arrays can only have elements of the same data type, whereas Python lists can have items of different data types.<\/p>\n
Python Lists have several methods for removing elements from the list.<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to remove elements from the list by values some of them are:<\/p>\n The remove method removes a list element based on its value rather than its index. When more than one element matches the provided value, the first matching element (the one with the lowest index) is removed. This is useful when you know which element needs to be removed ahead of time and don’t want to waste time looking for its index before removing it.<\/p>\n Let us now use the remove() function to remove the first instance of an element ‘is’.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If we use the remove() function to remove an element from a list that does not exist, what happens? In that case, Value Error will be returned.<\/p>\n To avoid the ValueError, we check to see if the value is already in the list and then remove it.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n As we saw in the previous examples, the remove() function always deletes the first occurrence of the given element from the list. To delete all occurrences of an element, we must use the remove() function in a loop until all occurrences are gone.<\/p>\n So we use while loop to achieve that.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Assume we have a list of numbers and another list of values that we want to remove from the first list. We want to remove all of the elements from list2 from list1.<\/p>\n We’ve created a separate function for this, which takes two different lists as arguments.<\/p>\n It deletes all occurrences from the original list for each element in the second list.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists are ordered sequences that can contain a wide range of object types. Members on lists can also be duplicated. Lists in Python are equivalent to arrays in other programming languages. There is, however, one significant difference. Arrays can only have elements of the same data type, whereas Python lists can have items of different …<\/p>\ngivenlist=[\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\" ,\"is\" ] , value=is<\/pre>\n
[\"hello\", \"this\", \"Btech\", \"Geeks\"]<\/pre>\n
Remove Elements from the List by Values<\/h2>\n
\n
Method #1:Using remove() function<\/h3>\n
# Function which removes the element for given value and returns list\r\ndef removeValue(givenlist, value):\r\n # removing the value using remove()\r\n givenlist.remove(value)\r\n # return the list\r\n return givenlist\r\n\r\n# Driver code\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\", \"is\"]\r\n# given value which is to be removed\r\nvalue = 'is'\r\n# passing list and value to removeValue function to remove the element\r\nprint(removeValue(givenlist, value))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks', 'is']<\/pre>\n
Method #2: Removing given value using remove() if exist<\/h3>\n
# Function which removes the element for given value and returns list\r\ndef removeValue(givenlist, value):\r\n # checking if value is present in givenlist\r\n if value in givenlist:\r\n # removing the value using remove()\r\n givenlist.remove(value)\r\n # return the list\r\n return givenlist\r\n\r\n# Driver code\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\", \"is\"]\r\n# given value which is to be removed\r\nvalue = 'is'\r\n# passing list and value to removeValue function to remove the element\r\nprint(removeValue(givenlist, value))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks', 'is']<\/pre>\n
Method #3:Removing all occurrences of\u00a0 given value<\/h3>\n
# Function which removes the element for given value and returns list\r\ndef removeValue(givenlist, value):\r\n # using while loop to remove all occurrences of given value\r\n while(value in givenlist):\r\n # removing the value using remove()\r\n givenlist.remove(value)\r\n # return the list\r\n return givenlist\r\n\r\n# Driver code\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\", \"is\"]\r\n# given value which is to be removed\r\nvalue = 'is'\r\n# passing list and value to removeValue function to remove the element\r\nprint(removeValue(givenlist, value))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks']<\/pre>\n
Method #4: Using values, remove all occurrences of multiple elements from a list.<\/h3>\n
\n
\n
# Function which removes the element for given value and returns list\r\ndef removeValue(givenlist, valuelist):\r\n # Traverse the value list\r\n for value in valuelist:\r\n # using while loop to remove all occurrences of given value\r\n while(value in givenlist):\r\n # removing the value using remove()\r\n givenlist.remove(value)\r\n # return the list\r\n return givenlist\r\n\r\n# Driver code\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\", \"is\"]\r\n# given value list which is to be removed\r\nvaluelist = ['is', 'hello', 'this']\r\n# passing list and valuelist to removeValue function to remove the element\r\nprint(removeValue(givenlist, valuelist))\r\n<\/pre>\n
['Btech', 'Geeks']<\/pre>\n
\n