Skip to content

Instantly share code, notes, and snippets.

@JulianWgs
Created April 22, 2020 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulianWgs/dbbacf7e7e268b6c6e2266f0a6e79b79 to your computer and use it in GitHub Desktop.
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)
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