ochensati ochensati - 2 months ago 6x
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']

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.


You need another list comprehension:

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