-1

Can you explain the second line with explain?

spam = ['a', 'z', 'A', 'Z']
spam.sort(key=str.lower)
print(spam)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • It does case-insensitive in-place sort of the list. The `key` argument is a callable which operates on the items and returns the object that will be used for sorting. In this case it returns the string converted to lower case. – alani Jun 14 '20 at 07:00
  • It is also worth mentioning that because elements `'a'` and `'A'` for example have equal keys returned by the `str.lower` callable, their relative order in the sorted list will be undefined. – alani Jun 14 '20 at 07:03
  • 2
    These things are [documented](https://docs.python.org/3/library/stdtypes.html?highlight=list.sort#list.sort) – donkopotamus Jun 14 '20 at 07:04
  • 1
    @alaniwi `list.sort` is a stable sort – donkopotamus Jun 14 '20 at 07:09
  • @donkopotamus Thank you, I stand corrected. Translation for OP: pairs like `'A'` and `'a'` that are equal when compared case-insensitively will appear in the same order that they did originally. – alani Jun 14 '20 at 07:13
  • Does this answer your question? [What does passing str.lower as the key to list.sort do?](https://stackoverflow.com/questions/50498997/what-does-passing-str-lower-as-the-key-to-list-sort-do) – Gino Mempin Feb 04 '23 at 13:19

6 Answers6

1

spam is a list, and lists in python have a built-in sort function that changes the list order from low to high values.

e.g.

Nums = [2,1,3]
Nums.sort()
Nums

Output

[1,2,3]

The key parameter of sort is a function that is applied to each element of the list before comparison in the sorting algorithm. str.lower is a function that returns characters in lowercase.

e.g.

A -> a

So, the second line sorts spam by the lowercase values of its elements.

Which should result in

[a,A,z,Z]
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
shaiel cohen
  • 41
  • 1
  • 9
0
spam.sort(key=str.lower)

is sorting your spam list alphabetically as it's case-insensitive. So if you change your 3rd line to

print(spam)

you get

['a', 'A', 'z', 'Z']

without the key in your sort the sorted list would be

['A', 'Z', 'a', 'z']
po.pe
  • 1,047
  • 1
  • 12
  • 27
0

spam.sort(key=str.lower)

sort() is a default function for sorting in python and it can take some sorting function as a parameter. here key = str.lower is the sorting function which means that it should parse every string to lower case before sorting. which means that this is case in sensitive search.

0

It performs case insensitive sorting.

Let's modify your example a bit, to include another entry "a":

spam = ['a', 'z', 'A', 'Z','a']

Naturally, you'd expect first "a" and second "a" to occur together. But they don't when you give key=str.lower, because of the properties of string ordering. Instead, a plain list.sort call will give you:

spam = ['a', 'z', 'A', 'Z','a']
spam.sort()
print(spam)

output
['A', 'Z', 'a', 'a', 'z']

On the other hand, specifying str.lower gives you this:

spam = ['a', 'z', 'A', 'Z','a']
spam.sort(key=str.lower)
print(spam)

output:
['a', 'A', 'a', 'z', 'Z']

Here, the original list elements are sorted with respect to their lowercased equivalents.

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
0

Basically, when str.lower is done then the data in spam becomes 'a','z','a','z' due to which when sorting is done then both 'a' will come first and then 'z' ... but as the actual spam is not altered and only while sorting the data's case was changed so you get the output as 'a' 'A' 'z' 'Z' .. same result you will get when you do key=str.upper.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Sandeep Kothari
  • 405
  • 3
  • 6
-1

I had never programming in Python, but what i see:

  • spam is array of strings
  • spam.sort = sorts array, key - is current value, you can do smth.
  • str.lower - means you will translate each string to lower case (L => l)
  • and last line means you returns that array (maybe...)