I need to remove all item[0]
if a
is == '1'
:
a = [['1','2','3'], ['2','4','9']]
for item in a:
if item[0] == '1':
del item
I need to remove all item[0]
if a
is == '1'
:
a = [['1','2','3'], ['2','4','9']]
for item in a:
if item[0] == '1':
del item
You can use a list comprehension as follows
a = [i for i in a if i[0] != '1']
Do not change length of list while iterating over it. make new list instead.
b = [i for i in a if i[0] != '1']
Use filter
:
new_a = list(filter(lambda item: item[0] != '1', a))
The list is just so its compatible regardless of your python version (filter
returns lazy sequence in python3).
A List Comprehension is the best way to solve this problem but if you want to use a for loop here's some Python code for that:
a = [[1,2,3], [2,1,9], [1,6,9], [5,6,7]]
# Code
def removeOneList(a):
for item in a:
if item[0] == 1:
del item[:]
return a
print(removeOneList(a))