Created
April 22, 2020 10:39
-
-
Save JulianWgs/dbbacf7e7e268b6c6e2266f0a6e79b79 to your computer and use it in GitHub Desktop.
Apply function to specific keys in python list (to for example censor text)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import copy | |
def apply_to_key(iterable, keys, func): | |
iterable = copy.deepcopy(iterable) | |
if isinstance(iterable, list): | |
for item in iterable: | |
iterable = clean(item, keys, func) | |
elif isinstance(iterable, dict): | |
for key, item in iterable.items(): | |
if key in keys: | |
item = func(item) | |
iterable[key] = clean(item, keys, func) | |
return iterable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment