-1

can anyone advise me how to sort the array by column 'age'?

student = {
     0 : {'name' : 'jane', 'age' : 40},
     1 : {'name' : 'pool', 'age' : 11},
     2 : {'name' : 'dave', 'age' : 28}
}

print(student[0])
print(student[1])
print(student[2])

results screen

{'name': 'jane', 'age': 40}
{'name': 'pool', 'age': 11}
{'name': 'dave', 'age': 28}

I tried

student = sorted(student, key=lambda student: student[2]) # sort by age not work

But it does not work nothing :-( Thank you for your help

-- EDIT -- Correct sort list (sorting age)

print(student[0])
print(student[1])
print(student[2])

results screen

{'name': 'pool', 'age': 11}    
{'name': 'dave', 'age': 28}
{'name': 'jane', 'age': 40}
Sat Net
  • 117
  • 2
  • 8

1 Answers1

3

These are all dicts, not lists, so they don't have order; for the inner dicts, they don't have numerical keys, so indexing them is an error. That said, you can convert the outer dict to a sorted list to get the result you want:

from operator import itemgetter

students = {
    0: {'name': 'jane', 'age': 40},
    1: {'name': 'pool', 'age': 11},
    2: {'name': 'dave', 'age': 28}
}

# Sort the sub-dicts by age and print
for student in sorted(students.values(), key=itemgetter('age')):
    print(student)

Note: Because the sub-dicts are also unordered, there is no guarantee that name appears before age in the output for each student. But this will output the students in the correct order.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • ShadowRanger: Thank you, I do not want to print just I want to adjust! – Sat Net Feb 02 '16 at 13:27
  • @SatNet: Well, you can store the result of `sorted` back to the name `student` (it will be a `list` of `dict`s at that point, not a `dict` of `dict`s) if you need to change the order but don't need to print yet. – ShadowRanger Feb 02 '16 at 13:34
  • ShadowRanger: student = sorted(student.values(), key=itemgetter('age')); so is it right ? Thank you. – Sat Net Feb 02 '16 at 13:58
  • Yeah. Though I'd probably name it `sortedstudents` or just replace the original `student` (unless the keys in `student` are meaningful, e.g. student ID numbers). If they are, you might want to actually write a proper class to represent students, rather than using a collection of `dict` as an ad-hoc organizational structure. – ShadowRanger Feb 02 '16 at 14:00
  • ShadowRanger : And you advise me please how to add data to the list? – Sat Net Feb 02 '16 at 14:00
  • After creating the `list`, it's like any other `list`. You can call `.append` to add new values; that won't preserve ordering, but if ordering is an issue, you can either call `.sort(key=itemgetter('age'))` on the `list` just before you need ordered data to resort it in place (Python's TimSort algorithm is optimized for cases where the data is already partially sorted, so the cost to do so is not as much as you might think), or (though it's annoying to use when you use a `key` for ordering), use [`bisect.insort`](https://docs.python.org/3/library/bisect.html#bisect.insort) to insert ordered. – ShadowRanger Feb 02 '16 at 14:04
  • @SatNet: Note that there is an [existing recipe for a `SortedCollection` that sorts on a given key and preserves sort order on subsequent insertion](http://code.activestate.com/recipes/577197-sortedcollection/) (also linked from the `bisect` documentation), that might be helpful if you need to store and retrieve student info retaining sorted order. – ShadowRanger Feb 02 '16 at 14:07
  • ShadowRanger :Very thank you very much for your help. – Sat Net Feb 02 '16 at 14:15