EMM
EMM

Reputation: 1812

Sorting a nested list in python

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.

Upvotes: 0

Views: 256

Answers (2)

Gareth Latty
Gareth Latty

Reputation: 89097

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.

Upvotes: 3

Abhijit
Abhijit

Reputation: 63777

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}, {}, {}]]
>>> 

Upvotes: 2

Related Questions