1

I have a 2d array like this one:

list_of_data = [
    ['Joe', 4, 4, 4, 5, 'cabbage', None], 
    ['Joe', 43, '2TM', 41, 53, 'cabbage', None],
    ['Joe', 24, 34, 44, 55, 'cabbage', None],
    ['Joe', 54, 37, 42, 85, 'cabbage', None],

    ['Tom', 7, '2TM', 4, 52, 'cabbage', None],
    ['Tom', 4, 24, 43, 52, 'cabbage', None],
    ['Tom', 4, 4, 4, 5, 'cabbage', None],

    ['Fred', 4, 4, 4, 5, 6, 'cabbage'],
    ['Fred', 4, 4, 4, 5, 6, 'cabbage'],
    ['Fred', 4, 4, 4, 5, 6, 'cabbage'],
]

I am interested in the rows containing the value '2TM' at its 2nd index. For example:

  • Joe has the value '2TM' at index 2 in the 2nd appearance of his data.
  • Tom has the value '2TM' at index 2 in the 1st appearance of his data.

Each time the value '2TM' appears in the data, I want to remove the next two rows. The example above would become the following:

list_of_data = 
    ['Joe', 4, 4, 4, 5, 'cabbage', None], 
    ['Joe', 43, '2TM', 41, 53, 'cabbage', None],

    ['Tom', 7, '2TM', 4, 52, 'cabbage', None],

    ['Fred', 4, 4, 4, 5, 6, 'cabbage'],
    ['Fred', 4, 4, 4, 5, 6, 'cabbage'],
    ['Fred', 4, 4, 4, 5, 6, 'cabbage'],
]

I've tried using list.pop like so:

for row[x] in list_of_data:
    if '2TM' in row:
        list_of_data.pop[x+1:x+2]
Zach Gates
  • 4,045
  • 1
  • 27
  • 51
TJE
  • 570
  • 1
  • 5
  • 20

2 Answers2

1

You would need to do something like this

list_of_data = [['Joe', 4, 4, 4, 5, 'cabbage', None], 
['Joe', 43,'2TM', 41, 53, 'cabbage', None],
['Joe', 24, 34, 44, 55, 'cabbage', None],
['Joe', 54, 37, 42, 85, 'cabbage', None],

['Tom', 7,'2TM', 4, 52, 'cabbage', None],
['Tom', 4, 24, 43, 52, 'cabbage', None],
['Tom', 4, 4, 4, 5, 'cabbage', None],

['Fred', 4, 4, 4, 5, 6, 'cabbage'],
['Fred', 4, 4, 4, 5, 6, 'cabbage'],
['Fred', 4, 4, 4, 5, 6, 'cabbage']]
x=0
for row in list_of_data:
    if '2TM' in row:
        list_of_data.pop(x+1)
        list_of_data.pop(x+1)
    x+=1
print(list_of_data)

You were quite close but just missed the increment of x.

Daniyal Ahmed
  • 715
  • 5
  • 11
  • `list.pop` will fail if there aren't two rows (or even one) after the row containing `'2TM'`. For example, if the last row (or second-to-last) of `list_of_data` contained a `'2TM'`, `list.pop` would raise an exception. – Zach Gates Aug 30 '17 at 01:41
  • He mentioned that next two rows need to be deleted. Although a check could be applied for last rows – Daniyal Ahmed Aug 30 '17 at 01:42
  • 1
    @ZachGates I understand your objection here. And for general purposes, you are right: this method would raise an objection in the scenario you describe. However, the inherent/unique nature of my dataset is that any time 2TM appears in my data, there are always 2 rows that need to be removed. So this .pop method actually worked for my data even as it may not work for general purposes. – TJE Aug 30 '17 at 01:58
1

Use a while loop:

index = 0

while index < len(list_of_data):
    if list_of_data[index][2] == '2TM':
        # check if the names are the same, as needed
        del list_of_data[index + 1:index + 3] 

    index += 1
Zach Gates
  • 4,045
  • 1
  • 27
  • 51
  • 1
    This methods works equally as well as the one below. Thanks very much for your input. – TJE Aug 30 '17 at 02:26