0

I need to dump the values of a dict to a list. In doing this, I want to preserve the sorting given when the dict is visualized.

Example. This is my dict:

mydict={10: 0, 20: 0, 30: 0, 40: 0, 50: 1.0, 60: 0, 70: 1.0, 80: 0.825, 90: 0.916, 100: 0.102}

I dump its values to a list like this:

y = mydict.values()

But when I visualize such list, this is what I get:

In[1]: y
Out[1]: [0.102, 1.0, 0, 0, 0.825, 1.0, 0, 0.916, 0, 0]

What can I do to make sure my list stores the values in the exact order shown by the dict? I want to have:

In[1]: y
Out[1]: [0, 0, 0, 0, 1.0, 0, 1.0, 0.825, 0.916, 0.102]

EDIT

If I use an OrderedDict while building my dict, the problem remains. My dict in this case would simply be visualized like this:

In[2]: mydict
Out[2]: OrderedDict([(100, 0.102), (70, 1.0), (40, 0), (10, 0), (80, 0.825), (50, 1.0), (20, 0), (90, 0.916), (60, 0), (30, 0)]

And y would still be the same as before:

In[3]: y
Out[3]: [0.102, 1.0, 0, 0, 0.825, 1.0, 0, 0.916, 0, 0]
FaCoffee
  • 7,609
  • 28
  • 99
  • 174

0 Answers0