Python >= 3.6 In python 3.6+, it seems that dict loading order is preserved by default without special dictionary types. The default Dumper, on the other hand, sorts dictionaries by key. Starting with pyyaml 5.1, you can turn this off by passing sort_keys=False: a = dict(zip("unsorted", "unsorted")) s = yaml.safe_dump(a, sort_keys=False) b = yaml.safe_load(s) assert list(a.keys()) == list(b.keys()
![In Python, how can you load YAML mappings as OrderedDicts?](https://cdn-ak-scissors.b.st-hatena.com/image/square/98d6f053a97a87156775f60757c60865d0f2c47d/height=288;version=1;width=512/https%3A%2F%2Fcdn.sstatic.net%2FSites%2Fstackoverflow%2FImg%2Fapple-touch-icon%402.png%3Fv%3D73d79a89bded)