Original:
a = [{ 1:1,2:2,3:3,4:4,5:5,1:1}]
for n, i in enumerate(a):
if i == 1:
a[n] = 10
First thing to know is, arrays start at 0. You have
if i == 1:
and so, your code will never execute.
You also have a duplicate key in your dictionary - 1 is used twice.
Since your dictionary is in a list, it will have to be index like:
a[i][j] = ...
where i refers to which element it is in the list, and j refers to which element in the dictionary.
Last, your i and n are reversed - enumerate puts the index in the first variable.
So, if I understand correctly what you want to accomplish, the end result should end up looking more like this:
a = [{1:1,2:2,3:3,4:4,5:5}]
for i, n in enumerate(a):
if i == 0:
a[0][1] = 10
print(a)
If you want to change the value for more than 1 key, then I might do something like this:
a = [{1:1,2:2,3:3,4:4,5:5}]
toChange = [[1,10], [4, 76]] # 1 and 4 are the keys, and 10 and 76 are the values to change them to
for i, n in enumerate(a):
if i == 0:
for change in toChange:
a[0][change[0]] = change[1]
print(a)
EDIT: Everything above is still correct, but as you and Tomerikoo pointed out, it does not quite answer the question. My apologies. The following code should work.
a = [{1: 1, 2: 2, 3: 3, 4: 4, 5: 5}]
toChange = [[1, 10], [4, 76]] # 1 and 4 are the keys, and 10 and 76 are the
values to change them to
for i, n in enumerate(a):
if i == 0:
for change in toChange:
try:
oldValue = a[0][change[0]]
del a[0][change[0]]
a[0][change[1]] = oldValue
except:
pass # handle it here
#This likely means you tried to replace a key that isn't in there
print(a)