I have part of a Python function that looks like:
for item in passedList:
tempDict = {}
tempDict ["first"] = item[0]
tempDict ["second"] = item[1]
tempDict ["third"] = item[2]
What I’m expecting back is:
{'first': 'item1', 'second': 'item2', 'third': 'item3'}
However, I get:
{'second': 'item2', 'first': 'item1', 'third': 'item3'}
It is probably a pretty simple oversight, but any thoughts on why this is happening?
This is because the implementation of dict in Python is a hashmap or hash table, which doesn’t store the elements in order.
You could use OrderedDict to get around this.