simplify to_python method.

Remove unused import, add some validation, change key checking syntax.