Here is another way to do this. This method works if we need to replace multiple subscriptions:
a=[1,3,5,10,13] a_sub=[3,5,10] b_sub=[9,7] def replace_sub(a, a_sub, b_sub): a_str = ',' + ','.join(map(str, a)) + ',' a_sub_str = ',' + ','.join(map(str, a_sub)) + ',' b_sub_str = ',' + ','.join(map(str, b_sub)) +',' replaced_str = a_str.replace(a_sub_str, b_sub_str)[1 : -1] return map(int, replaced_str.split(','))
Result:
>>> replace_sub(a, a_sub, b_sub) [1, 9, 7, 13] >>> replace_sub([10, 13, 4], [3, 4], [7]) [10, 13, 4] #[3,4] is not in the list so nothing happens
Replace a few subscriptions:
>>> a=[1,3,5,10,13,3,5,10] >>> a_sub=[3,5,10] >>> b_sub=[9,7] >>> replace_sub(a, a_sub, b_sub) [1, 9, 7, 13, 9, 7]