Sort dictionary into list

Viewed 5835

There are already a lot of questions about sorting dictionaries but I can't find the right answer to my question.

I have the dictionary v:

v = {3:4.0, 1:-2.0, 10:3.5, 0:1.0}

We have to turn the dictionary v into a sorted list.

lijst(v) = [1.0, -2.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.5]

I have tried working with this code:

def lijst(x):
    return sorted(x.items(), key=lambda x: x[1])

This is the list I receive:

lijst(v) = [(1, -2.0), (0, 1.0), (10, 3.5), (3, 4.0)]

Does anyone know how to convert this into a list of values sorted in order of their key, with the missing values padded with zero?

7 Answers
Related