Although @zquare had a better answer to this question, I feel like I need to call back using the Pythonic method, which will also take into account dictionary values โโthat are not strings. This is not a recursive look at you, since it only works with one-dimensional dictionary objects.
d.update({k: v.lstrip() for k, v in d.items() if isinstance(v, str) and v.startswith(' ')})
This updates the original dictionary value if that value is a string and starts with a space.
UPDATE: If you want to use regular expressions and avoid using start and end elements. You can use this:
import re rex = re.compile(r'^\s|\s$') d.update({k: v.strip() for k, v in d.items() if isinstance(v, str) and rex.search(v)})
This version is broken if the value has a leading or trailing space character.
source share