ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
w = ["bob", "Al", "al", "Bo"]
print(sorted(w), sorted(w, key=str.lower))
  1. A. ['al', 'Al', 'bob', 'Bo'] ['Al', 'al', 'Bo', 'bob']
  2. B. ['Al', 'Bo', 'al', 'bob'] ['al', 'Al', 'bob', 'Bo']
  3. C. ['Al', 'al', 'Bo', 'bob'] ['Al', 'al', 'Bo', 'bob']
  4. D. ['Al', 'Bo', 'al', 'bob'] ['Al', 'al', 'Bo', 'bob']

Correct answer: D. ['Al', 'Bo', 'al', 'bob'] ['Al', 'al', 'Bo', 'bob']

Default string order is by code point, so every capitalised word precedes every lowercase one. With key=str.lower, 'Al' and 'al' tie and the stable sort keeps their original order; 'bo' < 'bob' as a prefix.

More Python Programming questions

Exams that ask this topic