Oct-27-2023, 02:12 PM
I have the following structure:
my_list = [{"Dog": {"sound":"bark"}}, {"Cow": {"sound":"moo"}}, {"Cat": {"sound":"meow"}}]I want to get the following from this structure sorted or unsorted:
["Cat", "Cow", "Dog"] # or ["Dog", "Cow", "Cat"]The following ALMOST gives me what I want, but I'm getting a list of the complete dictionary instead of just the keys:
sorted(my_list, key=lambda x: x.keys[0]) [{'Cat': {'sound': 'meow'}}, {'Cow': {'sound': 'moo'}}, {'Dog': {'sound': 'bark'}}]Using Python 2.7, how can I get the list of the first (only) key from each item in my list of dictionaries?