0

I have a nested list:

a = [[{'aa': 2L}, {}, {'mm': 9L}, {}, {}], [{'aa': 1L}, {}, {'mm': 5L}, {}, {}], [{'aa': 2L}, {}, {'mm': 7L}, {}, {}], [{'aa': 5L}, {}, {'mm': 7L}, {}, {}]]

Desired Output:

a = [[{'aa': 1L}, {}, {'mm': 5L}, {}, {}], [{'aa': 2L}, {}, {'mm': 7L}, {}, {}], [{'aa': 5L}, {}, {'mm': 7L}, {}, {}], [{'aa': 2L}, {}, {'mm': 9L}, {}, {}]]

Output I am getting from a.sort() :

a = [[{'aa': 1L}, {}, {'mm': 5L}, {}, {}], [{'aa': 2L}, {}, {'mm': 7L}, {}, {}], [{'aa': 2L}, {}, {'mm': 9L}, {}, {}], [{'aa': 5L}, {}, {'mm': 7L}, {}, {}]]

Not desired.

Here I want to sort the list 'a' by considering any one of the keys of child lists.In this case I am using third dictionary and key 'mm'.Right now there is only one key 'mm' there may be multiple key value pairs but I should be able to avoid others and do the sorting on the basis of 'mm' keys value only.

2
  • 1
    As far as I see it, both the desired and a.sort() output are same(except for some missing whitespaces in desired output!) Commented Apr 18, 2012 at 11:35
  • It's unclear what you want to sort by here. I've given general advice, for something more tailored to your situation, edit and clarify a little. Commented Apr 18, 2012 at 11:37

2 Answers 2

3

You have a list of lists, so you need to sort each sublist if you want to order the sublists.

a_sorted = [sorted(sublist) for sublist in a]

Obviously, you can run sorted() on the list of sorted lists if you want the outer list to be sorted too.

You can pass the sorted() builtin a key argument - a function which takes the list item and returns the value to sort on. It's a little unclear exactly how you want to sort the list, you could clarify, although with this you should be able to work out a solution.

Your data structure seems a little odd, however. Where you have pairs in the form {key: value} you would normally be better off with a tuple: (key, value), or a single, larger dict that contains all of your pairs as keys and values, e.g:

a = [{'aa': 2L, 'mm': 7l}, {'aa': 2L, 'mm': 5L}, {'aa': 2L, 'mm': 9L}, {'aa': 2L, 'mm': 3L}]

In this case, we can use sorted(a, key=itemgetter("mm")) - using operator.itemgetter() - to sort on the value of 'mm'.

Or, if you need your empty pairs (without using keys to None, for example), as tuples:

a = [[('aa', 2L), (,), ('mm', 7L), (,), (,)], [('aa', 2L), (,), ('mm', 5L), (,), (,)], [('aa', 2L), (,), ('mm', 9L), (,), (,)], [('aa', 2L), (,), ('mm', 3L), (,), (,)]]

Here we can do a similar thing sorted(a, key=lambda sublist: sublist[2][1]) - we use lambda to make a quick function to extract the second item in the third item in the sublist.

If you wanted to keep your data structure as is - if, for example, you plan to expand the dictionaries with more content, then a similar plan would work sorted(a, key=lambda sublist: sublist[2]["mm"]) - this time using 'mm' to access in the dict.

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

Comments

2

I am not sure if I got the question correctly, but the answer seems simple to me as below.

Index the third item [3] in the key and then re-index the dictionary with key mm

>>> sorted(a,key=lambda key:key[2]['mm'])
[[{'aa': 1L}, {}, {'mm': 5L}, {}, {}], [{'aa': 2L}, {}, {'mm': 7L}, {}, {}], [{'aa': 5L}, {}, {'mm': 7L}, {}, {}], [{'aa': 2L}, {}, {'mm': 9L}, {}, {}]]
>>> 

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.