Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I am trying to sort a nested list in Python in O(n log n) time. I am not sure how to go about it, some help would be appreciated. The inner lists are being sorted by its sum.
My list:
[10, 7, [4,5], [1,2]]
My desired output:
[[1,2], 7, [4,5], 10]
input_list = [10, 7, [4,5], [1,2]] input_list.sort(key = lambda x: sum(x) if type(x) == list else x) [[1, 2], 7, [4, 5], 10]
Time complexity will be m*n*lg(n)
m*n*lg(n)
n = length of the input list
m = average length of nested list
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.