Search code examples
pythonlistdel

delete python list in the correct order


I tried to delete by this order: 11,12,13,21,22,23,31,32,33 and to stay with an empty list. at the begining I tried the regular deletion, but then I understood that you must use int for deletion and you can't you the object so, I started to use enumerate function but the I saw another problem. It was delete but not the whole list only part of it. Is there a way to delete the in this order?

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]

for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        jindex = jindex[j+1:]
    index = index[i+1:]
print(b)

print('\nnew try\n\n')

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        del jindex[j::]
    del b[i::]
print(b)

print('\nnew try\n\n')

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]

for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        del jindex[j]
    del index[i]
print(b)

print('\nnew try\n\n')

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]

for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        del b[i][j]
    del b[i]
print(b)

my output:

[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]

new try


[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[[], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[[], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[]

new try


[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[[], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
Traceback (most recent call last):
  File "/Users/asaf/PycharmProjects/first/openurl.py", line 28, in <module>
    del jindex[j]
IndexError: list assignment index out of range

Process finished with exit code 1

that is the result I'am looking for:

[[['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
[[['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
[[['21'],['22'],['23']],[['31'],['32'],['33']]]
[[['22'],['23']],[['31'],['32'],['33']]]
[[['23']],[['31'],['32'],['33']]]
[[['31'],['32'],['33']]]
[[['32'],['33']]]
[[['33']]]
[[]]

Solution

  • The issue is that you are iterating over your list while modifying it. This will often cause the exact problem you are encountering. Rather, you have have to iterate over indices (more like a classic-for-loop) and modify the list. Notice, though, you have to take into account that the index you are going to be deleting isn't the same as the index you iterate over. Rather, you are always deleting the first element of your sublist, and then the sublist itself in the outer-loop (except for the last iteration).

    >>> b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
    >>> for sublength in [len(sub) for sub in b]:
    ...   for _ in range(sublength):
    ...     print(b)
    ...     del b[0][0]
    ...   if len(b) > 1: # or else you'll end up with []
    ...     del b[0]
    ... 
    [[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
    [[['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
    [[['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
    [[['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
    [[['22'], ['23']], [['31'], ['32'], ['33']]]
    [[['23']], [['31'], ['32'], ['33']]]
    [[['31'], ['32'], ['33']]]
    [[['32'], ['33']]]
    [[['33']]]
    >>> print(b)
    [[]]
    >>>