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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -