I populate a python dictionary based on few conditions.
My question is:
can we retrieve the dictionary in the same order as it is populated?
questions_dict={}
data = str(header_arr[opt]) + str(row)
questions_dict.update({data : xl_data})
valid_xl_format = 7
if (type.lower() == "ma" or type.lower() == "mc"):
data = str(header_arr[opt]) + str(row)
questions_dict.update({data : xl_data})
valid_xl_format = 7
After populating if i iterate it is not in the order it is populated
for k in questions_dict:
logging.debug("%s:%s" %(k,questions_dict[k]))
To keep track of the order in which a dictionary is populated, you need a type different than dict
(commonly known as "ordered dict"), such as those from the third-party odict module, or, if you can upgrade to Python 2.7, collections.OrderedDict.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With