{"id":2856,"date":"2023-10-18T09:24:31","date_gmt":"2023-10-18T03:54:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=2856"},"modified":"2023-11-10T11:49:31","modified_gmt":"2023-11-10T06:19:31","slug":"python-remove-elements-from-list-by-index-or-indices","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-remove-elements-from-list-by-index-or-indices\/","title":{"rendered":"Python: Remove Elements from List by Index or Indices"},"content":{"rendered":"
Lists are ordered sequences that can contain a wide range of object types. Lists can also have duplicate members. Lists in Python can be compared to arrays in other programming languages. But there is one significant difference. Arrays can only contain elements of the same data type, whereas Python lists can contain items of various data types.<\/p>\n
Python Lists include a number of methods for removing items from the list.<\/p>\n
This article will go over various methods for removing a single or multiple elements from a 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 using Indexes or Indices some of them are:<\/p>\n <\/p>\n \u201cdel list object [index]\u201d can be used to exclude an item from a list based on its index location.<\/p>\n The del keyword, on the other hand, will raise IndexError if the list is empty or the specified index is out of range.<\/p>\n Output:<\/strong><\/p>\n It removed the element at position 2 . But what if we try to delete an element with an out-of-range index?<\/p>\n As an example,<\/p>\n we will delete the element at index 10(which doesn’t in this list) the output is:<\/p>\n Because the given index was out of bounds, it raised an IndexError. The index position was greater than the<\/p>\n list’s length. To avoid this index error, we should always check to see if the given index is valid.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The list class in Python has a function pop(index) that removes an item from the list at the given index.<\/p>\n However, if the list is empty or the given index is out of range, the pop() function may throw an IndexError.<\/p>\n As a result, we should exercise caution when using this function to delete an item from a list based on its index position.<\/p>\n We wrote a function that deletes an element from a list based on its index. It uses the pop() function internally,<\/p>\n but first checks to see if the given index is valid. Let us illustrate with an example:<\/p>\n Output:<\/strong><\/p>\n The function removeIndex() accepts two arguments,<\/p>\n In each of the previous solutions, we changed the list while it was still in place. Slicing, on the other hand, can be used to create a new list by removing the element at a given index from the original list.<\/p>\n To delete an element at index N, for example, divide the list into three parts.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Assume we have a list of 7 elements and want to remove the elements at indexes 1, 5, and 6. We can’t just name the pop after iterating over the specified index positions (index). The element at a given index will be removed by the pop() function, and the index location of all elements after the deleted elements will change as a result (decrease by 1).<\/p>\n The best approach is to sort the index positions in decreasing order and then call the pop() function on the highest to lowest index positions. To make this process as simple as possible, we’ve built a function.<\/p>\n Based on the specified index positions, it may remove multiple elements from a list.<\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists are ordered sequences that can contain a wide range of object types. Lists can also have duplicate members. Lists in Python can be compared to arrays in other programming languages. But there is one significant difference. Arrays can only contain elements of the same data type, whereas Python lists can contain items of various …<\/p>\ngivenlist=[\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"] , index=2<\/pre>\n
[\"hello\", \"this\", \"Btech\", \"Geeks\"]<\/pre>\n
Remove values from the List Using Indexes or Indices<\/h2>\n
\n
>Method#1: Using del keyword<\/h3>\n
# Function which removes the element at given index and returns list\r\ndef removeIndex(givenlist, index):\r\n # deleting the index\r\n del givenlist[index]\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\"]\r\n# given index which is to be removed\r\nindex = 2\r\n# passing list and index to removeIndex function to remove the element\r\nprint(removeIndex(givenlist, index))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks']<\/pre>\n
del givenlist[index]\r\nIndexError: list assignment index out of range<\/pre>\n
# Function which removes the element at given index and returns list\r\ndef removeIndex(givenlist, index):\r\n # deleting the index\r\n if(index < len(givenlist)):\r\n del givenlist[index]\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\"]\r\n# given index which is to be removed\r\nindex = 2\r\n# passing list and index to removeIndex function to remove the element\r\nprint(removeIndex(givenlist, index))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks']<\/pre>\n
>Method #2: Using pop() function<\/h3>\n
# Function which removes the element at given index and returns list\r\ndef removeIndex(givenlist, index):\r\n # using pop() function\r\n if index < len(givenlist):\r\n givenlist.pop(index)\r\n # returning the list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n# given index which is to be removed\r\nindex = 2\r\n# passing list and index to removeIndex function to remove the element\r\nprint(removeIndex(givenlist, index))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks']<\/pre>\n
\n
>Method #3: Using slicing<\/h3>\n
\n
# Function which removes the element at given index and returns list\r\ndef removeIndex(givenlist, index):\r\n # using slicing\r\n givenlist = givenlist[:index] + givenlist[index+1:]\r\n # returning the list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n# given index which is to be removed\r\nindex = 2\r\n# passing list and index to removeIndex function to remove the element\r\nprint(removeIndex(givenlist, index))\r\n<\/pre>\n
['hello', 'this', 'Btech', 'Geeks']<\/pre>\n
>Method #4 : Using indices to remove multiple elements<\/h3>\n
# Function which removes the element at given index and returns list\r\ndef removeIndex(givenlist, indices):\r\n # sorting indices list in reverse order\r\n indices = sorted(indices, reverse=True)\r\n # Traverse the indices list\r\n for index in indices:\r\n if index < len(givenlist):\r\n givenlist.pop(index)\r\n\r\n # returning the list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n# given indices list which are to be removed\r\nindices = [1, 2, 3]\r\n# passing list and indices list to removeIndex function to remove the element\r\nprint(removeIndex(givenlist, indices))\r\n<\/pre>\n
['hello', 'Geeks']<\/pre>\n
\n