If I have a list:
my_list = [3,2,2,3,4,1,3,4]
and a tuple
my_tuple = (3,5)
What's the best way of replacing elements in my_list using the tuple:
result = [5,2,2,5,4,1,5,4]
e.g.
for item in my_list:
if(item == my_tuple[0]):
item = my_tuple[1]
More generally, I would have a list of lists, and a list of tuples, and I would like to apply each of the tuples to each of the lists within the list of lists.
itemin the above is a copy!itemwill actually be a reference to the elements inmy_list, but sincemy_listcontains only immutable types it won't really matter. Ifmy_listwas a list of lists you could do something likeitem[:] = [1, 2]and modify the contents ofmy_list.