0

How can we splice a sequence with another sequence of the positions we want to splice?


For example, consider

a = [1,"A", 34, -123, "Hello", 12]
b = [0, 2, 5]

And the aim is to get:

[1, 34, 12]

The only solutions I could find was

c = []
for i in b:
   c.append(a[i])

Is there a solution that doesn't require to loop in Python?

jacobsa
  • 5,719
  • 1
  • 28
  • 60
Remi.b
  • 17,389
  • 28
  • 87
  • 168

1 Answers1

0

I believe the following is the shortest form possible:

c = [a[i] for i in b]
DJanssens
  • 17,849
  • 7
  • 27
  • 42