Python 101 : Sorting a List as Strings or as Integers


We have for example the below list:


When we try to sort it, we get the above error, since the list is a mix of integers and strings.

We could remedy that using a key that will tell the sorted() function to sort the list considering all the elements as a unique type.

For example, we can sort our list considering all its elements as strings:


We could also sort our list considering all its elements as strings:

Comments

Leave as a comment:

Archive