Use `operator.itemgetter(0)` instead of the lambda.  Also, if you set `reversed=True`, then you can pop from the tail on line 544 (more readable and an operation more suited for lists).