Ask coding questions

← Back to all posts
How to sort an iterable in python whilst keeping duplicate values
Dunce (69)

This code:

print(sorted([3, 3, 3, 2, 2, 2, 1, 1, 1,]))

outputs: [1, 2, 3]
But I would like it to keep the duplicate values, that is to say it should output something more like this: [1, 1, 1, 2, 2, 2, 3, 3, 3]
How would I do that?

Answered by Coder100 (18187) [earned 5 cycles]
View Answer
Comments
hotnewtop
Dunce (69)

@Coder100 Turns out some other issue is causing the duplicate values to be deleted. Thanks though!