{"id":5340,"date":"2021-05-07T11:14:39","date_gmt":"2021-05-07T05:44:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=5340"},"modified":"2021-11-22T18:42:53","modified_gmt":"2021-11-22T13:12:53","slug":"python-remove-elements-from-a-list-while-iterating","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-remove-elements-from-a-list-while-iterating\/","title":{"rendered":"Python: Remove Elements from a List while Iterating"},"content":{"rendered":"
A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed.<\/p>\n
The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list elements. The same value may appear multiple times in a list. Each event is regarded as a distinct element.<\/p>\n
Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to remove elements from the list while iterating some of them are:<\/p>\n To accomplish this, we must first make a copy of the list, and then iterate over that copied list. Then, for each element, we’ll decide whether or not to delete it. If so, use the remove() function to remove that element from the original list.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Using list comprehension, we can iterate over the list and choose which elements to keep in the new list. The new list can then be assigned to the same reference variable that was part of the original list.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Filter() function takes two arguments,<\/p>\n The first is a Lambda function, which can be any function. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed. The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list …<\/p>\ngivenlist = [\"hello\", \"this\", \"is\", \"this\", \"BTechGeeks\", \"this\", \"python\"]<\/pre>\n
['hello', 'is', 'BTechGeeks', 'python']<\/pre>\n
Delete Elements from a List while Iterating<\/h2>\n
\n
Method #1:Using for in loop<\/h3>\n
# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"this\", \"BTechGeeks\", \"this\", \"python\"]\r\n# given element which should be deleted\r\nkey = \"this\"\r\nfor element in list(givenlist):\r\n # checking if the element is equal to given key\r\n if(element == key):\r\n # using remove to remove element from list\r\n givenlist.remove(element)\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['hello', 'is', 'BTechGeeks', 'python']<\/pre>\n
Method #2:Using List Comprehension<\/h3>\n
# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"this\", \"BTechGeeks\", \"this\", \"python\"]\r\n# given element which should be deleted\r\nkey = \"this\"\r\n# using list comprehension to remove given element from the list\r\ngivenlist = [element for element in givenlist if element != key]\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['hello', 'is', 'BTechGeeks', 'python']<\/pre>\n
Method #3:Using filter() function<\/h3>\n
\nThe second list is the one from which we want to remove elements.
\nIt iterates through all of the list’s elements, applying the given function to each one. It returns the elements for which the given function returns True during iteration. So we can use the filter() function to iteratively filter elements from a list.<\/p>\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"this\", \"BTechGeeks\", \"this\", \"python\"]\r\n# given element which should be deleted\r\nkey = \"this\"\r\n# using filter() function to remove given element from the list\r\ngivenlist = list(filter(lambda element: element != key, givenlist))\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['hello', 'is', 'BTechGeeks', 'python']<\/pre>\n
\n