dict now keeps the storage order in python3.7, but the pprint output is sorted automatically

I posted it in commemoration because it triggered the update of python. It's a language that is currently being updated.

--Referenced site

By the way, the operation check is Python3.7.3-> Python3.8.5.

`` `dict``` keeps the storage order in python 3.7 and above

Very convenient! However, when printing with `` pprint```, the benefit is not so good due to the ** pprint``` ** sort before output ** specification.

test1.py


from pprint import pprint

d = {1: 10, 9: 90, 3: 30, 7: 70, 5: 50}
print(d)
pprint(d)

out1


{1: 10, 9: 90, 3: 30, 7: 70, 5: 50}
{1: 10, 3: 30, 5: 50, 7: 70, 9: 90}

The same applies when substituting one by one.

test2.py


from pprint import pprint

d2 = {}
d2[0] = 0
d2[8] = 80
d2[2] = 20
d2[6] = 60
d2[4] = 40
print(d2)
pprint(d2)

out2


{0: 0, 8: 80, 2: 20, 6: 60, 4: 40}
{0: 0, 2: 20, 4: 40, 6: 60, 8: 80}

`sort_dicts = False``` can be specified for `pprint``` in python3.8 or later

From python3.8, it seems that the `sort_dicts``` option has been added to pprint```. The default is `` sort_dicts = True, so if you want to keep the order, you need to specify `` `False.

test3.py


from pprint import pprint

d = {1: 10, 9: 90, 3: 30, 7: 70, 5: 50}
print(d)
pprint(d, sort_dicts=False)

out3


{1: 10, 9: 90, 3: 30, 7: 70, 5: 50}
{1: 10, 9: 90, 3: 30, 7: 70, 5: 50}

Recommended Posts

dict now keeps the storage order in python3.7, but the pprint output is sorted automatically
[Python] How to output the list values in order
Automatically get the port where Arduino is stuck in Python
[python] What is the sorted key?
What is "mahjong" in the Python library? ??
Output the number of CPU cores in Python
What is wheezy in the Docker Python image?
About the difference between "==" and "is" in python
Output in the form of a python array
It is easy to execute SQL with Python and output the result in Excel
Check if the string is a number in python
Output the time from the time the program was started in python
Get the result in dict format with Python psycopg2
Find the part that is 575 from Wikipedia in Python
Solution if the module is installed in Python but you get an error in Jupyter notebook