I have a list like this:
a_list = [['a', 1], ['b', 4], ['c', None]]
I need to replace all None
values with 0
and replace any value that is not None
to None
. So above list will become:
modified_a_list = [['a', None], ['b', None], ['c', 0]]
I have code like this:
a_list = [['a', 1], ['b', 4], ['c', None]]
b_list = a_list
modified_a_list = []
for item in b_list:
if item[1]==None:
item[1]=0
modified_a_list.append(item)
else:
item[1] = 0
modified_a_list.append(item)
print a_list, modified_a_list
My output becomes:
a_list = [['a', None], ['b', None], ['c', 0]]
modified_a_list = [['a', None], ['b', None], ['c', 0]]
modified_a_list
looks OK. But why a_list
is changed as well?