-1

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
martineau
  • 112,593
  • 23
  • 157
  • 280
  • This is not the correct way to ask questions , mind the typos, give some details instead of just posting some random thoughts in order to get proper help – Doodle Mar 21 '18 at 15:10

4 Answers4

3

You can use a list comprehension as follows

a = [i for i in a if i[0] != '1']
Cory Kramer
  • 107,498
  • 14
  • 145
  • 201
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']
Rahul
  • 10,000
  • 4
  • 47
  • 84
0

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).

Reut Sharabani
  • 29,003
  • 5
  • 68
  • 85
0

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))
wolfbagel
  • 448
  • 2
  • 10
  • 21