[Python] How to replace existing key in a dictionary in python?

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I want to replace the specific existing key at index 1 in a dictionary in python.

    from collections import OrderedDict
    regDict= OrderedDict()
    regDict[("glenn")] = 1
    regDict[("elena")] = 2
    print("dict",regDict)


    prints:

    dict OrderedDict([('glenn', 1), ('elena', 2)])


    target output:

    dict OrderedDict([('glenn', 1), ('new', 2)]) # replacing key in index 1

    Continue reading...

Compartilhe esta Página