For idx, (val, cnt) in enumerate(item_count.iteritems(),1): This code question

Asked 2 years ago, Updated 2 years ago, 16 views

python

2022-09-20 18:57

1 Answers

>>> item_count = { "a":3, "b":2, "c":1 }
>>> for idx, (val, cnt) in enumerate(item_count.iteritems(), 1):
    print (f"{idx}th | {val} is {cnt})


Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    for idx, (val, cnt) in enumerate(item_count.iteritems(), 1):
AttributeError: 'dict' object has no attribute 'iteritems'


>>> for idx, (val, cnt) in enumerate(item_count.items(), 1):
    print (f"{idx}th | {val} is {cnt})


1st | a has 3
2nd | 2 for b
3rd | c is 1


2022-09-20 18:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.