Algorithm For Pop Operation In Python