4

So I have two lists:

num = [
    3, 22, 23, 25, 28, 29, 56, 57, 67, 68, 73, 78, 79, 82, 83, 89, 90, 91,
    92, 98, 99, 108, 126, 127, 128, 131, 132, 133
]
details = [
    'num=10,time=088', 'num=10,time=084', 'num=10,time=080', 'num=10,time=076',
    'num=10,time=072', 'num=10,time=068', 'num=10,time=064', 'num=10,time=060',
    'num=10,time=056', 'num=10,time=052', 'num=10,time=048', 'num=10,time=044',
    .
    .
    .
    .
    'num=07,time=280', 'num=07,time=276', 'num=05,time=508', 'num=05,time=504',
    'num=05,time=500', 'num=05,time=496'
]

num has 28 elements and details has 134 elements. I want to remove elements in details by index based on values from num. For example elements with index 3, 22, 23, 25, 28... (these are numbers from num list) should be removed from details.

When I use .pop() as it is described here it gives me an error saying:

AttributeError: 'str' object has no attribute 'pop'

similarily when I use del details[] it gives me an error saying:

IndexError: list assignment index out of range

Here is my code:

for an in details:
    an.pop(num)
martineau
  • 119,623
  • 25
  • 170
  • 301
seyet
  • 1,080
  • 4
  • 23
  • 42

2 Answers2

8

This should do what you want (delete from details every element indexed by the values in num):

for i in reversed(num):
    del details[i]

It iterates over the list backwards so that the indexing of future things to delete doesn't change (otherwise you'd delete 3 and then the element formerly indexed as 22 would be 21)--this is probably the source of your IndexError.

jtniehof
  • 581
  • 3
  • 8
  • 7
    Fine as long as the indices in `num` are in ascending order...as they are in the OP's example, but that aspect of them isn't mentioned (and is therefore not be a given). Perhaps `sorted(num, reverse=True)` would be a better choice in that sense. – martineau May 04 '18 at 18:52
0

Hmm. Two things. First your loop not quite right. Instead of

for an in details:
an.pop(num)

you want

for an in num:         # step through every item in num list
    details.pop(an)    # remove the item with index an from details list

Second, you need to make sure to pop() items from details in reverse order so that your indexes are good. For example if you pop() index 3 from details, then everything else in details is reordered and when you go to remove index 22 it will be the wrong "cell".

I've simplified details to be a list containing numbers 0 to 133, but this code should work just fine on your real list

num = [3, 22, 23, 25, 28, 29, 56, 57, 67, 68, 73, 78, 79, 82, 83, 89, 90, 91, 92, 98,
99, 108, 126, 127, 128, 131, 132, 133]

details = list(range(134))


# sort indexes in descending order (sort in place)
num.sort(reverse = True)

for an in num:
    details.pop(an)

print(details)

output

[0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24,
26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 58, 59, 60, 61, 62, 63, 64, 65, 66, 69, 70, 71,
72, 74, 75, 76, 77, 80, 81, 84, 85, 86, 87, 88, 93, 94, 95, 96, 97, 100, 101, 10
2, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 11
9, 120, 121, 122, 123, 124, 125, 129, 130]
bfris
  • 5,272
  • 1
  • 20
  • 37