x = {'s': 1, 'd': 2, 'f': 4}
x['s'] = 6
print(x)
for q, w in enumerate(x):
print(q, w)
The above code is giving different outputs when printing it directly and when using enumerate and printing it.
{'s': 6, 'd': 2, 'f': 4}
0 s
1 d
2 f
x = {'s': 1, 'd': 2, 'f': 4}
x['s'] = 6
print(x)
for q, w in enumerate(x):
print(q, w)
The above code is giving different outputs when printing it directly and when using enumerate and printing it.
{'s': 6, 'd': 2, 'f': 4}
0 s
1 d
2 f
You have to iterate through it with dict.items
, enumerate
just gives the index.
So try this:
for q, w in x.items():
print(q, w)
As U12-Forward was saying you were printing indices and not the values. You could also print the value as in the code down below. But as he says, if you want to iterate over keys and values you should use items()
x = {'s': 1, 'd': 2, 'f': 4}
x['s'] = 6
print(x)
for q, w in enumerate(x):
print(q, w, x[w])
>>
0 s 6
1 d 2
2 f 4