-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
  • 119,623
  • 25
  • 170
  • 301
  • 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 – Nothing 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
  • 114,268
  • 16
  • 167
  • 218
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,830
  • 4
  • 53
  • 88
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
  • 30,449
  • 6
  • 70
  • 88
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
  • 468
  • 2
  • 11
  • 21