0

_Hello, world! I have the problem with removing elements from multiple list. I have a multiple list.

print("Preparation")
print(dateTableMonthValues)

It prints me:

[['4:10'], ['3:20'], ['3:45'], ['32:05'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['35:00'], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], ['28:45'], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50'], [], ['35:50']]

From this array I need to remove several elements, for example ['32:05'], ['35:00'], ['28:45'], [], ['35:50']]

Ok, I have found these values and:

for i in range(len(dateTableDayValues)):
    week = dateTableDayValues[i][0]
    print(dateTableDayValues[i][0])
    if week[3] == 'w':
        print('w')
        dateTableMonthValues[i].pop(0)
print("Check")
print(dateTableMonthValues)

Now it prints me:

[['4:10'], ['3:20'], ['3:45'], [], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], [], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], [], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50'], [], []]

So, I have two questions. Fisrtly, why method 'pop' didn't remove the elements, cause I still have elements like this: '[]'. Secondly, how can I deleted from array (list) these empty elements '[]' for defining the sum of all elements? It's not working:

if dateTableMonthValues[i][0] == []:
         del dateTableMonthValues[i][0]
         dateTableMonthValues[i].pop(0)

Old question: So, when I trying to access list[3][0] it has wrote me "list index out of range". So I decided that the element was removed.

But anyway I couldn't to define the sum of all my elements. I still continue get the message

IndexError: list index out of range

Help me please how can I solve the problem?

Sergo
  • 93
  • 3
  • 10
  • This question is really hard to understand. You should show the actual code, what you expect, what you are getting instead, and the question you want to ask. We have no idea what you mean by (or what code you are running with) `define the sum` given a list like `[['4:10'], ['3:20'], ['3:45'], [], ['5:00']]` – Mark Apr 23 '22 at 21:23
  • In your example, `list[3]` is an empty list. It doesn't have a first item, so `list[3][0]` is out of range. – John Gordon Apr 23 '22 at 21:31
  • Hi, Mark and John Gordon, I listened your advices and edited my question. – Sergo Apr 24 '22 at 08:15

1 Answers1

2

There are many ways you can handle it!

Edit: (the question was a little modified)

  • Why method 'pop' didn't remove the elements? Because you access the element 'i' and delete his first element. If you want to Delete it, you can do:
dateTableMonthValues.pop(i) # Not: dateTableMonthValues[i].pop(0)

OR

dateTableMonthValues.remove(the_element)
  • And If you want to delete each [ ] of the list, you can do:
lst = [['4:10'], ['3:20'], ['3:45'], [], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], [], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], [], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50'], [], [],[],[],[]]
i = 0
while i<len(lst):
    if lst[i] == []:
        lst.pop(i)
    else: 
        i+=1
print(lst) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50']]

Enumerate

l = [['4:10'], ['3:20'], ['3:45'], ['Delete me'], ['5:00'], ['Delete me'], ['10:00']]

for i,e in enumerate(l):
    if e == ["Delete me"]:
        l.pop(i)

print(l) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['10:00']]

But if you don't really know how to use enumerate:

Try/Except

l = [['4:10'], ['3:20'], ['3:45'], ['Delete me'], ['5:00'], ['Delete me'], ['10:00']]

for i in range(len(l)):
    try:
        if l[i] == ["Delete me"]:
            l.pop(i)
    except IndexError:
        pass

print(l) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['10:00']]

To make it easy: When there is an "IndexError" the program just pass as nothing happened. (In reality, it's more complex from the system point of view but you can interpret it like that!)

Remove

l = [['4:10'], ['3:20'], ['3:45'], ['Delete me'], ['5:00'], ['Delete me'], ['10:00']]

for e in l:
    if e == ["Delete me"]:
        l.remove(e)

print(l) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['10:00']]

Last but not least! list.remove(element) remove the element from the list and not the element by an index like pop

TKirishima
  • 742
  • 6
  • 18
  • This is a good answer, you deserve my +1 – FLAK-ZOSO Apr 23 '22 at 21:28
  • TKrishima, thank you for your answering, but, sorry, I asked the wrong question. I have fixed it. – Sergo Apr 24 '22 at 08:17
  • @Sergo I modified my answer :) – TKirishima Apr 24 '22 at 10:34
  • TKirishima, thank you for your kindness and smartness:) My problem was connected with this problem of empty cells (https://stackoverflow.com/questions/38442634/googlesheet-apiv4-getting-empty-cells). But i found it and solved the problem. You helped me, thanks!) – Sergo Apr 25 '22 at 17:17