Change all elements in a list of indices corresponding to another list

Viewed 345

I have 2 lists:

b = ['zero', 'one', 'two']
c = [0, 0, 0, 1, 1, 2, 1, 0, 2, 0]

Is there an elegant way to change every element in c and get

c = ['zero', 'zero', 'zero', 'one', 'one', 'two', 'one', 'zero', 'two', 'zero'] 
3 Answers

I think just

c = [b[i] for i in c]

would do the trick

c = map(lambda i: b[i],c)

Using map

Another way using itemgetter:

from operator import itemgetter

itemgetter(*c)(b)

Output:

('zero', 'zero', 'zero', 'one', 'one', 'two', 'one', 'zero', 'two', 'zero')
Related