Simple, just pop on the list item.

>>> a = [[1,2,3,4], [6,7,8,9]]
>>> a[1].pop(3)
>>> a
[[1, 2, 3, 4], [6, 7, 8]]
Answer from nathancahill on Stack Overflow
🌐
Plus2Net
plus2net.com › python › list-2d.php
Python 2D List: Create, Access, Rows & Columns | plus2net
Use pop() to remove rows or column elements. Avoid [[0] * n] * n when independent rows are required. zip(*my_list) can transpose a rectangular 2D list. The sum() function can calculate diagonal totals of a square matrix-like list.
🌐
Reddit
reddit.com › r/learnpython › how to remove multidimensional list items based on individual elements matching?
r/learnpython on Reddit: How to remove multidimensional list items based on individual elements matching?
September 11, 2022 -

Let's say I have the following:

array = [[20 , 'apples' , 1] , [25 , 'pears' , 3] , [50 , 'grapes' , 100] , [9 , 'apples' , 99]]

I'm trying to remove the duplicates when the second item matches... in this case "apples" so and updated array would look like the following (keeping the first instance and removing the rest:

[20 , 'apples' , 1]

['25' , 'pears' , 3]

['50' , 'grapes' , '100']

I've looked around but all I'm finding is information for deleting duplicates from 1 dimensional lists but can't seem to find anything for multidimensional lists based on what I'm needing to match/remove.

My actual code has several thousand entries like this and the single item duplicates are causing issues with some sorting/ranking reporting I am trying to generate.

Any suggestions or recommendations would be much appreciated. Thank you

🌐
Stack Overflow
stackoverflow.com › q › 42100983
removing element from 2d list - false error - Python - Stack Overflow
The error message is confusing in this case because it uses the variable x, which isn't related to your 'x' strings. From the python docs: list.remove(x): Remove the first item from the list whose value is x.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-remove-given-element-from-list-of-lists
Python | Remove given element from list of lists - GeeksforGeeks
May 5, 2023 - Use the map() function to apply the "remove_element" function to each sublist in the input list and convert the resulting map object to a list. ... # Python3 code to demonstrate # Removing element from list of lists # using map() and filter() ...
🌐
AlgoTree
algotree.org › algorithms › a pinch of code
Python : Two dimensional list :: AlgoTree
Python3 : Deleting items in a two dimensional list · # Declare a two dimensional list _list_2d = [ [ 'A', 'New', 'Beginning' ], [ 'Right', 'Here' ], ['ABC', 'XYZ'] ] # Print each list in a two dimensional list print ("Two dimensional list...") for _list in _list_2d: print (_list) # Delete individual elements in a 2 dimensional list del _list_2d[0][0] # After deletion, the 0'th item in the 2d list becomes [ 'New', 'Beginning' ] del _list_2d[0][1] # This would delete item at index 1 in the first list i.e 'Beginning'. del _list_2d[2] # This would delete the third item in the two dimensional list print ("Two dimensional list after deletion of items...") for _list in _list_2d: print (_list)
Top answer
1 of 5
11

Just for the sake of showing a much simpler way of doing this using list comprehensions, the sorted method and slicing:

d = [[1, 3, 4], [2, 4, 4], [3, 4, 5]]

n = [sorted(l)[1:-1] for l in d]

print(n)

# [[3], [4], [4]]

Some reading material on each of the items used to solve this problem:

  • list comprehension
  • sorted
  • slicing

To take care of duplicates, this answer by Padraic is very well done.

2 of 5
3

If you want to remove all occurrences, you will have to find the min and max and remove all occurrence from each sublist:

def remove(l):
    for sub in l:
        t = {min(sub), max(sub)}
        sub[:] = (ele for ele in sub if ele not in t)


l = [[1, 3, 4], [1, 2, 4, 4], [3, 4, 5]]

remove(l)

Which will give you:

[[3], [2], [4]]

To find the min and max in a single pass you can use a helper function:

def min_max(sub):
    # all numbers are > float("-inf") and < float("inf")
    mx, mn = float("-inf"), float("inf")
    for ele in sub:
        if ele < mn:
            mn = ele
        if ele > mx:
            mx = ele
    return {mn, mx}

def remove(l):
    for sub in l:
        # find min and max
        mn_mx = min_max(sub)
        # update sublist so all occurrences of either are removed
        sub[:] = (ele for ele in sub if ele not in mn_mx)

Even if your own logic worked and you wanted to remove all the elements equal to the max, it would not work using remove as it will only remove the first occurrence each time.

In [8]: l = [1,2,3,4,4]

In [9]: l.remove(4)

In [10]: l
Out[10]: [1, 2, 3, 4]

Based on one of your comments you seem to have strings in your sublists which will error when compared to an int, if the string is always the first element you can slice it off:

   from itertools import islice

   def remove(l):
        for sub in l:
            sub = sub[1:]
            mn_mx = min_max(sub)
            sub[:] = (ele for ele in sub if ele not in mn_mx)
🌐
Mrgoff
mrgoff.com › py_lists
Python toolbox MrGoff.com
If you know the value of an item you wish to remove you can use the dot remove notation to perform this action.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove an Item from a List in Python: remove, pop, clear, del | note.nkmk.me
April 17, 2025 - In Python, you can remove items (elements) from a list using methods such as remove(), pop(), and clear(). You can also use the del statement to delete items by index or slice. Additionally, list comp ...
🌐
Dirask
dirask.com › snippets › python › python remove element in 2d list
❤ 💻 python remove element in 2d list - Dirask
Python · [Edit] + 0 · - 0 · OneCricketer · 460 · Copy · 1 2 3 4 5 6 7 8 9 10 11 12 my_list = [ [11, 12, 13], [21, 22, 23], [31, 32, 33] ] row_index = 0 column_index = 0 del my_list[row_index][column_index] # delete element 11 print(my_list) # [[12, 13], [21, 22, 23], [31, 32, 33]] Append snippet · Donate to Dirask ·
🌐
Nanyang Technological University
libguides.ntu.edu.sg › python › deleteelementsfromarrays
NP.10 Deleting elements from arrays - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
Learn practical Python programming skills for basic data manipulation and analysis. ... Let's start with this array in x and delete the 2nd and 4th elements. To do so, we simply need to specify the positions in np.delete. Now lets say we have a 2D array. Removing elements will no longer be as easy as removing them from 1D arrays.
🌐
DataCamp
datacamp.com › tutorial › python-remove-item-from-list
How to Remove an Item from a List in Python: A Full Guide | DataCamp
August 1, 2024 - In the first case, we can use the remove() function. # Initialize a list with integers from 1 to 5 my_list = [1, 2, 3, 4, 5] # Removes element 5 from the list my_list.remove(5) print(my_list) # Expected output: [1, 2, 3, 4]