Transforming and filtering Python dictionaries