home ~ socials ~ projects ~ rss

Convert A Dictionary Into A Sorted List In Python

March 2022
genres = {
	"Junk": [1],
	"Chill": [2],
	"Dance": [3],
	"Jass": [4], 
	"A": [7],
	"B": [1],
	'a': [3],
	'c': [2], 
	"Adf": [],
	"Aeee": [],
	"aeeed": [],
	"Aerer": []
}

items = [{"id": f, "data": genres[f]} for f in sorted(genres.keys(), key=lambda x: x.lower())]

print(items)
end of line
Share link:
https://www.alanwsmith.com/en/26/ea/k6/im/?convert-a-dictionary-into-a-sorted-list-in-python