For example, I got a tuple
a = ('chicken', 1, 'lemon', 'watermelon', 'camel')
I want to build a new tuple b from a but change a little bit
d = {0: 'apple', 1: 'banana', 2: 'lemon', 3: 'watermelon'}
b = (a[0], d[a[1]], a[2], a[3], a[4])
another way to do it
b = list(a)
b[1] = d[b[1]]
b = tuple(b)
All of them work, but look silly.
Is there another elegant way to do this job? or are there some skills to modify the original tuple?
This is a bit inefficient in comparison to yours because it does a check on every item, but the general idea is that it looks at each item in your
a, checks for a matching key indand if it finds one, returns the value at that key. If no key is found, the original item is returned: