Ordering a Dictionary in Versions below 3.7.x

I’m trying to create a dictionary from elements in a list. The list elements are read from a text file. The dictionary created has unordered key value pairs. I’m working on python 2.6.4 on redhat linux .

ll = ['modu1', 'path1', 'modu2', 'path2', 'modu3', 'path3']
it = iter(ll)
Lt = zip(it,it)
Op = dict(Lt)

expected Op should be {'modu1:path1 ,modu2:path2, modu3:path3} but i’m getting Op as { modu2:path2, modu3:path3, modu1:path1}

Use OrderedDict from collections. But really, stop using Python 2.6.4. Its support ended a decade ago.

3 Likes

I tried this on python3.8 and 2.7. It does not work on 2.7.
Op = dict(sorted(Op.items()))