shorten the encode function

It's more pythonic now! :)