Source

x = {'a': 1, 'b': 2, 'd': 3, 'c': 4} d = list(x.keys()) for key in sorted(d): print(key + '=>' + d[key]) 

Mistake

 Traceback (most recent call last): File "C:/Python projects/qq.py", line 4, in <module> print(key + '=>' + d[key]) TypeError: list indices must be integers or slices, not str 

Read Mark Lutz - Python, 5th Edition and ran into a small problem. I use python 3.6.

Closed due to the fact that off-topic participants jfs , ߊߚߤߘ , br3t , mymedia , Raz Galstyan Oct 21 '17 at 18:44 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - ߊߚߤߘ, br3t, Raz Galstyan
  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - jfs, mymedia
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Please press the edit button, paste the error text and your code there, select it and press {} among the editor’s buttons or Ctrl-K on the keyboard - mymedia

2 answers 2

There is a suspicion that there should be x[key] , and not d[key] , otherwise the code does not make much sense.

    In variable d you have a list. Access to the elements of the list is carried out by a numerical index. For example d[0] . But in the key cycle variable, you do not have numbers, but letters — a, b, c, and d.

    • There is a suspicion that there should be x [key], and not d [key] ... - Vladimir Martyanov
    • @ Vladimir Martianov, the answers - in the answers. - Qwertiy