Getting the keys and values of a dictionary in the original order as a list

Hello,

You can use from collections import OrderedDict or wait for Python 3.7 to come to Rhino (maybe never…) 8.3. collections β€” High-performance container datatypes β€” Python 2.7.18 documentation

And please don’t use list and dict as variable names because you are overwriting the builtin names :wink:

from collections import OrderedDict

my_dict = OrderedDict([("1","a"),("2","b"),("3","c")])
my_dict["4"] = "d"

print my_dict
print my_dict.keys()

Yields:

OrderedDict([(β€˜1’, β€˜a’), (β€˜2’, β€˜b’), (β€˜3’, β€˜c’), (β€˜4’, β€˜d’)])
[β€˜1’, β€˜2’, β€˜3’, β€˜4’]

Have a great week!

3 Likes