Is a dict really an ordered dict? can we iter point by point?

Iain Duncan's icon

It looks to me like the order of keys in a dict is safe (not the case in Python), so we can reliably iterate through a dict and always get the same order. I'm curious as to why there is no built in step-by-step iterator for dict though, as there is with coll. My question is whether treating the contents of a dict as an ordered collection is safe to do, and if so, if one has to do this by hacking up your own iterator by getting the keys with a getkeys message and keeping track of your index yourself. Or maybe I'm missing something obvious. Or maybe one should dump to coll to do this?

thanks
Iain

Francesco Del Bello's icon

Hi, I'm currently having the same issue, has someone resolved/found a workaround for this?

11OLSEN's icon

The items will be in the order you've written them into the dict. To be sure you could save an index with your items?

Francesco Del Bello's icon

The problem is that i may have to change the index for a given key, but i know i cannot change the keys' name in a dict... Maybe a solution is to put the index as a value for every item, other than the value itself?

ex:
{
    "Dict Name" :     {
        "Item Name" : ["index", "value"],
        "Item Name" : ["index", "value"],
        "Item Name" : ["index", "value"]
}
}

11OLSEN's icon

Yes. Or use a coll where it's easy to change the index.

Francesco Del Bello's icon

Yeah that makes sense
Thank you!!

Francesco