0

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]

1 Answer 1

2
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)

n = length of the input list

m = average length of nested list

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.