Se7enSquared
10/18/2019 - 5:51 PM

Zip 2 dictionaries

Interlace the key/values of a dictionary together

stocks = { 
	'Goog' : 520.54, 
	'FB' : 76.45, 
	'yhoo' : 39.28, 
	'AMZN' : 306.21, 
	'APPL' : 99.76
	} 

zipped_1 = zip(stocks.values(), stocks.keys()) 

# sorting according to values 
print(sorted(zipped_1)) 

zipped_2 = zip(stocks.keys(), stocks.values()) 
#sorting according to keys 
print(sorted(zipped_2)) 


# output
#[(39.28, 'yhoo'), (76.45, 'FB'), (99.76, 'APPL'), (306.21, 'AMZN'), (520.54, 'Goog')]
#[('AMZN', 306.21), ('APPL', 99.76), ('FB', 76.45), ('Goog', 520.54), ('yhoo', 39.28)]