Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
Comment

python filter dictionary by keys

# Basic syntax:
{key: your_dict[key] for key in your_dict.keys() and {'key_1', 'key_2'}}
# Where this uses list comprehension for dictionaries to make a new dictionary
#	with the keys that are found in the set

# Example usage:
your_dict = {'key_1': 1, 'key_2': 2, 'key_3': 3}
{key: your_dict[key] for key in your_dict.keys() and {'key_1', 'key_2'}}
--> {'key_1': 1, 'key_2': 2}
Comment

filter dict by list of keys python

dict_you_want = { your_key: old_dict[your_key] for your_key in your_keys }
Comment

filter dict with certain keys

>>> obj = { "a":1, "b":{"c":2,"d":3}}
>>> only(obj,["a","b.c"])
{'a': 1, 'b': {'c': 2}}
Comment

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
Comment

python filter dictionary

dict
Comment

filter dictionary python

friends = [
    {'name': 'Sam', 'gender': 'male', 'sport': 'Basketball'},
    {'name': 'Emily', 'gender': 'female', 'sport': 'Volleyball'}
]
# filter by column names
print([{key: friend[key] for key in friend.keys() if key in ["name", "sport"]} for friend in friends])
# filter by rows
print([a for a in friends if a["name"] in ["Sam"]])
Comment

PREVIOUS NEXT
Code Example
Python :: how to multiply a string in python 
Python :: django regexvalidator example 
Python :: python bit shift by 3 
Python :: how to make a column a factor in pandas 
Python :: grouped bar chart matplotlib 
Python :: python dictionary get 
Python :: generate dates between two dates python 
Python :: pandas groupby mean round 
Python :: python plot groupby colors 
Python :: scikit learn pca 
Python :: check type of django messages 
Python :: Python JSON API example 
Python :: check if a value is nan pandas 
Python :: with suppress python 
Python :: train test split sklearn 
Python :: python json check if key exists 
Python :: python find difference between lists 
Python :: requests save data to disk 
Python :: python - count total numeber of row in a dataframe 
Python :: django in conda 
Python :: calculate mean median mode in python 
Python :: if main 
Python :: (for in) printing in python 
Python :: python multiplication array 
Python :: how to download a .xlsx file from google colab 
Python :: Python, importing other scripts from other directories 
Python :: pow python 
Python :: hstack 
Python :: how to install ffmpeg python heroku 
Python :: Compute the 2d histogram of x and y. 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =