-1
a = [["A","B"],["C","D"],["D","E"]]

for i in range(len(a)):
  if i == 0:
    a[i] = []

print(a) # [[], ['C', 'D'], ['D', 'E']]

I used above to remove inside list from a list but it prints [] . I want to remove the list itself.

Desired Output :- [['C', 'D'], ['D', 'E']]

Another approach which I tried as below works for i=0, i=1 and even i=2 But when my condition is all true ( kind of one case) then,

a = [["A","B"],["C","D"],["D","E"]]

for i in range(len(a)):
  if True:
    a.pop(i)

print(a) # IndexError: pop index out of range

I understood that using pop is shifting the indexes and causing the error as out of range. Need solution to tackle this.

Beth Mckey
  • 17
  • 6
  • 1
    Regarding 'using pop is shifting the indexes and causing the error as out of range': You can avoid that problem by iterating in reverse order: `for i in reversed(range(len(a))):` – H. Doebler Aug 25 '21 at 06:42
  • I have conditions in place and have to verify to check, if satisfies then only remove the specific list. I used loop to iterate over all inner list and then verify based on the condition to remove that specific list or if all conditions are satisfied , remove all lists. – Beth Mckey Aug 25 '21 at 06:42
  • It is better to have a look at https://wiki.python.org/moin/TimeComplexity#list – Rinshan Kolayil Aug 25 '21 at 06:43
  • You should probably write the range as `range(len(a)-1)` – PCM Aug 25 '21 at 06:52

2 Answers2

4

It's straightforward:

del a[0]

Of course, when you remove an element from a list, the indices for the remaining elements change.

It sounds like you want to remove all elements that meet some conditions, for example:

a = [4, 5, 1, 2, 8, 0]

# remove all the elements < 5 (i.e. only leave the ones >= 5):
result = [x for x in a if x >= 5]

A list comprehension would be the most pythonic and simple way to go about that.

If your condition only applies to the indices, not the values, that can be done with a comprehension as well, for example, to remove all the elements at odd indices (leaving the ones at even indices):

a = [4, 5, 1, 2, 8, 0]
result = [x for n, x in enumerate(a) if n % 2 == 0]
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    I am also trying for if True ( when all conditions satisfies). It throws index out of range error. – Beth Mckey Aug 25 '21 at 06:35
  • @BethMckey No it will **not**. Your code throws index out of range because when the elements gets popped out, indexes will change. – TYeung Aug 25 '21 at 06:35
  • `del a[0]` would be more straightforward – Kelly Bundy Aug 25 '21 at 06:36
  • 1
    this will work only if all condition satisfies but when one of the condition doesn't like ( if i == 1 ) it still removes 0th index list but here it should remove 1st index (["C","D"]) only. – Beth Mckey Aug 25 '21 at 06:40
  • @BethMckey,so you want to delete lists dynamically and selectively? – PCM Aug 25 '21 at 06:52
2

You could just use slicing:

>>> a = [["A","B"],["C","D"],["D","E"]]
>>> a = a[1:]
>>> a
[['C', 'D'], ['D', 'E']]
>>> 

Or use the del keyword:

>>> a = [["A","B"],["C","D"],["D","E"]]
>>> del a[0]
>>> a
[['C', 'D'], ['D', 'E']]
>>> 

Or just list.pop:

>>> a = [["A","B"],["C","D"],["D","E"]]
>>> a.pop(0)
['A', 'B']
>>> a
[['C', 'D'], ['D', 'E']]
>>> 

Or with unpacking:

>>> a = [["A","B"],["C","D"],["D","E"]]
>>> _, *a = a
>>> a
[['C', 'D'], ['D', 'E']]
>>> 

Edit for iterating:

Try enumerate:

>>> a = [["A","B"],["C","D"],["D","E"]]
>>> [v for i, v in enumerate(a) if i != 0]
[['C', 'D'], ['D', 'E']]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114