[Python] Difference between dictionary and OrderedDict

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

  1. Stack

    Stack Membro Participativo

    I am trying to get a sorted dictionary. But the order of the items between mydict and orddict doesn't seem to change.

    from collections import OrderedDict

    mydict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

    orddict = OrderedDict(mydict)

    print(mydict, orddict)

    # print items in mydict:
    print('mydict')
    for k, v in mydict.items():
    print(k, v)

    print('ordereddict')
    # print items in ordered dictionary
    for k, v in orddict.items():
    print(k, v)


    # print the dictionary keys
    # for key in mydict.keys():
    # print(key)


    # print the dictionary values
    # for value in mydict.values():
    # print(value)

    Continue reading...

Compartilhe esta Página