ochensati ochensati - 3 months ago 16
Python Question

"list indices must be integers, not list" with enumerate

I have this code

cNames = data["channelnames"]
goodChannels = [i for i,x in enumerate(cNames) if x!='Skipped']
data["channelnames"]=cNames[goodChannels]


I need the channel numbers later, but the cNames[goodChannels] throws the error "list indices must be integers, not list"

the list is made of integers. Is there a way to make this work correctly?

cNames[np.array(goodChannels)] does not work with the same error, so I am assuming that there is something else going on here.

Answer

You need another list comprehension:

data["channelnames"] = [cNames[i] for i in goodChannels]