Return sorted dictionary of first n elements from a dictionary

PHOTO EMBED

Wed Oct 19 2022 08:00:25 GMT+0000 (Coordinated Universal Time)

Saved by @hasib404 #python

freq = {3: 3, 2: 6, 1: 4}
k = 2

sort_orders = {k: freq[k] for k in sorted(freq, key=freq.get, reverse=True)[:k]}
print(sort_orders)

# Output {2: 6, 1: 4}
content_copyCOPY

https://stackoverflow.com/questions/20944483/python-3-sort-a-dict-by-its-values