Iteration in python dictionary

I populate a python dictionary based on several conditions.

My question is:

Is it possible to find a dictionary in the same order in which it is filled?

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 filling, if I iterate, this is not the order in which it is filled.

  for k in questions_dict: logging.debug("%s:%s" %(k,questions_dict[k])) 
+4
source share
3 answers

To keep track of the filling order of a dictionary, you need a type other than dict (usually called an "ordered dict"), such as a third-party odict or, if you can upgrade to Python 2.7, collections.OrderedDict .

+8
source

Dictionaries are not ordered collections. You must have some other data to keep track of the order.

+2
source

smart dictionaries are useful in that keys are mapped to objects called hashes, and dictionaries internally store your values โ€‹โ€‹associated with key hashes. therefore, when you iterate over your records, this is due to the order of the hashes, not the source keys.

0
source

All Articles