Answers for "python dictionary filter keys"

3

only keep few key value from dict

>>> dict_filter = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
>>> large_dict = {"a":1,"b":2,"c":3,"d":4}
>>> new_dict_keys = ("c","d")
>>> small_dict=dict_filter(large_dict, new_dict_keys)
>>> print(small_dict)
{'c': 3, 'd': 4}
>>>
Posted by: Guest on September-18-2020
0

python filter a dictionary

d = dict(a=1, b=2, c=3, d=4, e=5)
print(d)
d1 = {x: d[x] for x in d.keys() if x not in ['a', 'b']}
print(d1)
Posted by: Guest on January-14-2021
0

filter dict by list of keys python

dict_you_want = { your_key: old_dict[your_key] for your_key in your_keys }
Posted by: Guest on March-26-2020
1

filter dict

{k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}
Posted by: Guest on June-12-2020
0

filter dict with certain keys

def only(object,keys):
    obj = {}
    for path in keys:
        paths = path.split(".")
        rec=''
        origin = object
        target = obj
        for key in paths:
            rec += key
            if key in target:
                target = target[key]
                origin = origin[key]
                rec += '.'
                continue
            if key in origin:
                if rec == path:
                    target[key] = origin[key]
                else:
                    target[key] = {}
                target = target[key]
                origin = origin[key]
                rec += '.'
            else:
                target[key] = None
                break
    return obj
Posted by: Guest on June-22-2021

Python Answers by Framework

Browse Popular Code Answers by Language