I have a list of elements that I want to sort, but I do not want to sort them, only those who have a certain state. For example, let's say I have a list of students:
lst = [Mary, John, Anna, Peter, Laura, Lisa, Steve]
Some of them have a job, say [Anna, Lisa, Steve]. I want to sort (ascending) them by the number of hours of work and move them to the top of the list, while preserving everything else in the same order. Therefore, we say that the number of hours of work:
Anna.job.hours # 10
Lisa.job.hours # 5
Steve.job.hours # 8
After partial sorting, the list will look like this:
[Lisa, Steve, Anna, Mary, John, Peter, Laura]
Of course, I could create two new lists from the original, sort the one I want to sort, and put them together to achieve what I want:
with_job = [person for person in lst if person.job]
without_job = [person for person in lst if not person.job]
with_job.sort(key=lambda p: p.job.hours)
lst = with_job + without_job
But I wonder if he has a frightening, Putin's way of doing it.