Hi,I don’t understand the output of the program below:
my_list = [[25], [15, 25, 35], [10, 15]]
sorted_list = sorted(my_list, key=max)
print(sorted_list)
OUTPUT: [[10, 15], [25], [15, 25, 35]]
Then, if key=min, the output is
Thanks
Hi,I don’t understand the output of the program below:
my_list = [[25], [15, 25, 35], [10, 15]]
sorted_list = sorted(my_list, key=max)
print(sorted_list)
OUTPUT: [[10, 15], [25], [15, 25, 35]]
Then, if key=min, the output is
Thanks
They’re both in ascending order, but ordered by either the largest or smallest element in the sub list.
The large element is 35?
Yes, the largest, in fact. That’s why when sorted by key max, the sub-list with 35, in appears last.
Where is the list comprehension?
Hello, @mctaby, and welcome to the Python community!
Please see the following pages for useful information about this forum:
Your Python code and its output would be easier to read if it were formatted for proper display. The first of those two pages offers guidance regarding how to format Python code and output whenever you ask questions or help other users here. Specifically, that information is offered in the section How do I post code snippets, error messages etc?.
In what manner does your output differ from what you expected? With that information, we can can best provide effective assistance.
Yes, the title mentions a list comprehension, but that is not what we have here.
I fixed the title. We (other than author) cannot edit posts, but we can edit thread title.