function - Collecting values from a Python dictionary with list of keys -
dict = {'w1': 56, 'w2': 19, 'w3': 77, 'w4': 45, 'w5': 31} mywords = ['w1','w4','w5'] outputlist=[] items in mywords: tmps = dict[items] outputlist.append(tmps)
my question can we, without using for
loop, collect values (output) dictionary particular list ("myword")?
this operator.itemgetter
for:
>>> import operator >>> dict = {'w1': 56, 'w2': 19, 'w3': 77, 'w4': 45, 'w5': 31} >>> mywords = ['w1','w4','w5'] >>> operator.itemgetter(*mywords)(dict) [56, 45, 31]
Comments
Post a Comment