Filter dict to contain only certain keys?

后端 未结 15 932
耶瑟儿~
耶瑟儿~ 2020-11-22 10:52

I\'ve got a dict that has a whole bunch of entries. I\'m only interested in a select few of them. Is there an easy way to prune all the other ones out?

相关标签:
15条回答
  • 2020-11-22 11:37

    Here's an example in python 2.6:

    >>> a = {1:1, 2:2, 3:3}
    >>> dict((key,value) for key, value in a.iteritems() if key == 1)
    {1: 1}
    

    The filtering part is the if statement.

    This method is slower than delnan's answer if you only want to select a few of very many keys.

    0 讨论(0)
  • 2020-11-22 11:37

    Code 1:

    dict = { key: key * 10 for key in range(0, 100) }
    d1 = {}
    for key, value in dict.items():
        if key % 2 == 0:
            d1[key] = value
    

    Code 2:

    dict = { key: key * 10 for key in range(0, 100) }
    d2 = {key: value for key, value in dict.items() if key % 2 == 0}
    

    Code 3:

    dict = { key: key * 10 for key in range(0, 100) }
    d3 = { key: dict[key] for key in dict.keys() if key % 2 == 0}
    

    All pieced of code performance are measured with timeit using number=1000, and collected 1000 times for each piece of code.

    For python 3.6 the performance of three ways of filter dict keys almost the same. For python 2.7 code 3 is slightly faster.

    0 讨论(0)
  • 2020-11-22 11:39

    Given your original dictionary orig and the set of entries that you're interested in keys:

    filtered = dict(zip(keys, [orig[k] for k in keys]))
    

    which isn't as nice as delnan's answer, but should work in every Python version of interest. It is, however, fragile to each element of keys existing in your original dictionary.

    0 讨论(0)
  • 2020-11-22 11:39

    Here is another simple method using del in one liner:

    for key in e_keys: del your_dict[key]
    

    e_keys is the list of the keys to be excluded. It will update your dict rather than giving you a new one.

    If you want a new output dict, then make a copy of the dict before deleting:

    new_dict = your_dict.copy()           #Making copy of dict
    
    for key in e_keys: del new_dict[key]
    
    0 讨论(0)
  • 2020-11-22 11:42

    Short form:

    [s.pop(k) for k in list(s.keys()) if k not in keep]
    

    As most of the answers suggest in order to maintain the conciseness we have to create a duplicate object be it a list or dict. This one creates a throw-away list but deletes the keys in original dict.

    0 讨论(0)
  • 2020-11-22 11:45

    This one liner lambda should work:

    dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
    

    Here's an example:

    my_dict = {"a":1,"b":2,"c":3,"d":4}
    wanted_keys = ("c","d")
    
    # run it
    In [10]: dictfilt(my_dict, wanted_keys)
    Out[10]: {'c': 3, 'd': 4}
    

    It's a basic list comprehension iterating over your dict keys (i in x) and outputs a list of tuple (key,value) pairs if the key lives in your desired key list (y). A dict() wraps the whole thing to output as a dict object.

    0 讨论(0)
提交回复
热议问题