Get elements from dict using multiple keys

后端 未结 2 417
我寻月下人不归
我寻月下人不归 2021-01-27 03:42

If I have:

d = {\'one\':1, \'two\':2, \'three\':3, \'four\':4}

How can I get values of \'one\' and \'three\' in one command. Something like thi

2条回答
  •  鱼传尺愫
    2021-01-27 04:19

    Using list comprehension:

    >>> d = {'one':1, 'two':2, 'three':3, 'four':4}
    >>> [d[key] for key in 'one', 'three']
    [1, 3]
    

提交回复
热议问题