I have a list of dictionary like this [{1: {'Name': 't1', 'seq': 1}}, {3: {'Name': 't3', 'seq': 3}}, {2: {'Name': 't2', 'seq': 2}}]
And I want to sort this list based on just the key (and not the value of that key)
Expected output is
[{1: {'Name': 't1', 'seq': 1}}, {2: {'Name': 't3', 'seq': 2}}, {3: {'Name': 't2', 'seq': 3}}]
I know this can be done by getting key as list then sort them and then create another list, but i am looking for more elegant may be a one liner solution
CodePudding user response:
You can use the key arg at sorted that gets a function to sort docs: https://docs.python.org/3/library/functions.html#sorted
sorted_lst = sorted(lst, key=lambda x: list(x.keys())[0])