Postgresql While Loop Exit In Python List