Printing out a dictionary fails #12085
-
Running this code in Python: # my data dictionary
my_data_dict = {'Time': '00:00', 'Battery voltage': 26.5, 'PV current': 10.00, 'Load current': 4.00, 'Daily WH in': 0.00, 'Daily WH out': 0.00, 'State of charge': 90, 'Battery temperature': 20.00, 'Error': 20480}
for value in my_data_dict.values():
print(f'value: {value}') prints out the values in the correct order, which I thought was pretty clever. However, doing the same on my ESP32 with v1.20 the order gets jumbled. Is there something I need to do to be more explicit in Micropython? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
MicroPython was originally based on Python 3.4. The behavior you're seeing in Python where dictionaries maintain their insertion order was added in Python 3.7. On most builds, we do support collections.OrderedDict, but you won't be able to use a dict literal to assign it. It would be possible to make MicroPython match CPython here (and I believe there is a PR to do so) but it comes at a small overhead which seems wasteful for the majority of cases where you do not care about the insertion order. |
Beta Was this translation helpful? Give feedback.
MicroPython was originally based on Python 3.4. The behavior you're seeing in Python where dictionaries maintain their insertion order was added in Python 3.7.
On most builds, we do support collections.OrderedDict, but you won't be able to use a dict literal to assign it.
It would be possible to make MicroPython match CPython here (and I believe there is a PR to do so) but it comes at a small overhead which seems wasteful for the majority of cases where you do not care about the insertion order.