0
dist=[a,b,c,d,e]
spset=[1,3]
k=[]
for m in range(1,self.n+1):
    if m not in spset:
        k+=dist[m] 

I'm trying to make a list k that contains the all elements of dist except the ones with whose indexes are in spset[]. What am I doing wrong? The error is:

k+=dist[m] 
TypeError: 'int' object is not iterable
1
  • Briefly: a_list = [1,2]; a_list += [3] results in a_list being [1, 2, 3]. If you have 3 and not [3], you should use a_list.append(3). Mar 10, 2017 at 9:45

2 Answers 2

1

The problem might be the one stated by @SuperSaiyan. Also, here you have another possible solution more compact and simple:

[x for i,x in enumerate(dist) if i not in spset]
0

Because dist[m] is probably an int. You are trying to "Extend" a list through the += operation. You probably want k.append(dist[m]).

Not the answer you're looking for? Browse other questions tagged or ask your own question.