Wayne Sum
Wayne Sum

Reputation: 25

How do I show the order while print keys from a dictionary?

account = {"USD": 10, "HKD": 1000}
for key in account.keys():
   print(key)

I would like the output to be like:

  1. USD
  2. HKD

In my full program, there are other account with different number of currencies, so I cannot fix 1./2. before keys. I have tried enumerate(), but it can't be used while accessing keys from dictionary.

Upvotes: 0

Views: 36

Answers (2)

Phillyclause89
Phillyclause89

Reputation: 680

I believe this is what you want.

account = {"USD": 10, "HKD": 1000}

for n, key in enumerate(account, start=1):
   print(f'{n}. {key}')

Example in python tutor

Upvotes: 1

yanxun
yanxun

Reputation: 626

for index, key in enumerate(your_dict.keys()):
    print(index, key)

Upvotes: 0

Related Questions