I\'m loading data into a python dict, pulled from coinmarketcap\'s api and then I want to be able to sort it by the rank. I\'ve had a look online and although I\'ve seen some ex
You can't order a dict
, but you can create a list of the items
and have that sorted:
sorted(data.iteritems(), key=lambda x:x[1][u'rank'])
Using collections.OrderedDict
Demo:
from collections import OrderedDict
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'])) )
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'], reverse=True)) ) #Descending order
Output:
OrderedDict([(1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'})])
OrderedDict([(825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'})])