-1
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 
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • not sure what is your question but keep in mind that enumerate function returns a tuple where the first value is an index 0 based and the second argument since it is a dictionary it iterate over the keys – Orenico Aug 22 '21 at 10:39

2 Answers2

1

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)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

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
chatax
  • 990
  • 3
  • 17