[Python] How do I print a sorted Dictionary in Python 3.4.3

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I need to print a dictionary sorted alphabetically by key and the print should include the associated value.

    I can print alphabetically sorted Keys and I can print sorted values but not alphabetically sorted keys with the values attached.

    My code:

    class1 = { 'Ethan':'9','Ian':'3','Helen':'8','Holly':'6' } # create dictionary

    print(sorted(class1)) # prints sorted Keys
    print(sorted(class1.values())) # Prints sorted values


    I need to print sorted keys with values

    for k,v in class1.items():
    print(k,v) # prints out in the format I want but not alphabetically sorted

    Continue reading...

Compartilhe esta Página