Reputation: 176
_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?
Upvotes: 0
Views: 84
Reputation: 761
There are many ways you can handle it!
Edit: (the question was a little modified)
dateTableMonthValues.pop(i) # Not: dateTableMonthValues[i].pop(0)
OR
dateTableMonthValues.remove(the_element)
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
Upvotes: 2