1

I need to delete an item in a lists of list, based on the value in the inner list.

Example data:

persons = [ ['p1', 12, 45], ['p2', 10, 88] ['p3', 39, 66], ['p4', 34, 88], ['p5', 20, 16]]

I need to delete an item that has a value of 88, L1[x][3] == 88.

This is my code:

    for i in range(len(persons)):
        if persons[i][2] > longivity:
           del persons[i]

I have used del L1(i), L1.remove[I] and a few others. I tried to use list comprehension, but I could get it to work correctly. I have tried so many variations, I'm quite lost. I tried several This is what I'm getting:


Traceback (most recent call last):ulation: 2 Murders: 0 Food supply: 104.0
  File "/Users/bigd/Documents/Python/Projects/Simulators/first_sim.py", line 172, in <module>
    year, longivity, infantMortality, food, persons, world, work_age, murders = runYear(
  File "/Users/bigd/Documents/Python/Projects/Simulators/first_sim.py", line 144, in runYear
    persons, murders = deadpool(longivity, year, murders, persons)
  File "/Users/bigd/Documents/Python/Projects/Simulators/first_sim.py", line 109, in deadpool
    if persons[i][2] > longivity:
IndexError: list index out of range
BigD
  • 31
  • 6
  • Couldn't help but notice you wrote "L1[x][**3**] == 88" in the question, but you also have `persons[i][2]` in the code block. Perhaps you just made a mistake between 2 and 3, since the error is an `IndexError`. – nullromo Jun 09 '22 at 22:51
  • Yes, typo. Sorry Question about the L1 = [x for x in L1 if x[2] != 88] Is this deleting an entry that is greater than 88? – BigD Jun 09 '22 at 23:02

0 Answers0