[Python] How to get the index with the key in a dictionary?

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have the key of a python dictionary and I want to get the corresponding index in the dictionary. Suppose I have the following dictionary,

    d = { 'a': 10, 'b': 20, 'c': 30}


    Is there a combination of python functions so that I can get the index value of 1, given the key value 'b'?

    d.??('b')


    I know it can be achieved with a loop or lambda (with a loop embedded). Just thought there should be a more straightforward way.

    Continue reading...

Compartilhe esta Página