Aditya Aditya - 1 month ago 9
Python Question

Sort dict keys into list

I have a dictionary containing IP addresses and hd space for each IP.

{'': '7.3G', '': '3.5G', '': '7.4G', '': '17G'}

I want to take three IPs with the greatest space and put them into a list. Is this possible?


Slice off the trailing 'G' part from the values and convert them to float in the sort key:

ips = {'': '7.3G', '': '3.5G', '': '7.4G', '': '17G'}

sorted_ips = sorted(ips, key=lambda x: float(ips[x][:-1]), reverse=True)[:3]
# ['', '', '']