Sort list of strings alphabetically

Doni picture Doni · Jun 16, 2016 · Viewed 24.7k times · Source

So i have a question, how can i sort this list:

['Pera','mela','arancia','UVA']

to be like this:

['arancia','mela','Pera','UVA']

In the exercise it said to use the sorted() function with the cmp argument.

Answer

kardaj picture kardaj · Jun 16, 2016

You need to sort your elements based lowercase representation of the strings:

sorted(['Pera','mela','arancia','UVA'], key=str.lower)

this will output:

['arancia', 'mela', 'Pera', 'UVA']