1

I'm trying to select some elements from a string list given a list of indexes. For example:

String=['a','b','c']
Id = [1,2]
s = String [Id]

It gives the error :

list indexes must be integers or slices, not list.

How can I resolve it?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Pancio
  • 23
  • 5
  • 1
    what do you want to get? `['b', 'c']`? – buran Feb 15 '22 at 08:57
  • you can only index by 1 index, for a list of indexes you need to get a list back. for a continuous stretch of indexes read about slicing, for discontinued you could construct a new list containing only those indexes. – Patrick Artner Feb 15 '22 at 08:57

1 Answers1

0

You can do:

String=['a','b','c']
Id = [1,2]
s = [String[id] for id in Id] 
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Orius
  • 1,093
  • 5
  • 11