Question discussion

What is the output of the following Python code?w = ["bb", "a", "ccc", "dd"] print(sorted(w, key=len, reverse=True))

2137Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

w = ["bb", "a", "ccc", "dd"]
print(sorted(w, key=len, reverse=True))
  • A['a', 'bb', 'dd', 'ccc']
  • B['ccc', 'dd', 'bb', 'a']
  • C['ccc', 'bb', 'dd', 'a']
  • D['ccc', 'bb', 'a', 'dd']
Correct answer: C

Explanation

sorted() is stable even with reverse=True, so the equal-length strings "bb" and "dd" keep their original order: ccc, bb, dd, a.

Discussion 0

Nobody has replied yet. Start the discussion!