[Python] How to sort a dictionary based on a list in python

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I have a dictionary

    a = {'ground': obj1, 'floor 1': obj2, 'basement': obj3}


    I have a list.

    a_list = ['floor 1', 'ground', 'basement']


    I want to sort dictionary a using its keys based on the list. Is it possible to do that?

    i.e.:

    sort(a).based_on(a_list) #this is wrong. But I want something like this.


    The output doesn't have to be another dictionary, I don't mind converting the dictionary to tuples and then sort those.

    Continue reading...

Compartilhe esta Página