Compress function in python