I have a list of tuples (lot):
lot = [('490001', 'A-ARM1'),
('490001', 'A-ARM2'),
('490002', 'B-ARM3')]
Subsequently, I loop through every second tuple element and wish to replace every 'A-' and 'B-' characters by a dictionary value:
from more_itertools import grouper
dict = {'A-': 'ZZ', 'B-': 'XX'}
for el1, el2 in lot:
for i, j in grouper(el2, 2):
if i+j in dict:
lot2 = [ (el2.replace( (i+j), dict[i+j] )) for el1, el2 in lot ]
print(lot2)
After 'lot2 = ' something is going wrong, my output is
['A-ARM1', 'A-ARM2', 'XXARM3']
instead of [ZZARM1', 'ZZARM2', 'XXARM3']
Can anyone give me a hint on how to resolve this? Also, if you think I can write this more elegantly, feel free to let me know. I'm eager to learn. Any help is greatly appreciated.
This might not be the shortest solution, but it should work: