Python basic dict sort order

Print Dict as a matter of course

$ python 
>>> a = {}
>>> a["b"] = 1
>>> a["d"] = 2
>>> a["c"] = 3
>>> a["a"] = 4
>>> print a
{'a': 4, 'c': 3, 'b': 1, 'd': 2}

→ It's disjointed. ..

How about list

$ python 
>>> a = []
>>> a.append(1)
>>> a.append(2)
>>> a.append(3)
>>> a.append(4)
>>> a.append(9)
>>> a.append(8)
>>> print a
[1, 2, 3, 4, 9, 8]

→ This seems OK.

I used Dict in the order I registered it. ..

There seems to be an OrderedDict!

$ python 
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d["b"] = 1
>>> d["d"] = 2
>>> d["c"] = 3
>>> d["a"] = 4
>>> print d
OrderedDict([('b', 1), ('d', 2), ('c', 3), ('a', 4)])

→ You can do it.

Summary

Where you don't really care about python? However, I had such a habit. Until sharing.

reference) http://docs.python.jp/2/tutorial/datastructures.html#tut-dictionaries

A dictionary is a set of unordered key: value pairs ...

Recommended Posts

Python basic dict sort order
[Python] Sort
Python # sort
RF Python Basic_01
Basic Python writing
Python Dict category
Python3 basic grammar
RF Python Basic_02
Sort list elements in a specified order in Python
Python basic course (12 functions)
Python I'm also basic
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Basic sorting in Python
Bubble sort in Python
Python basic course (9 iterations)
Python self-made class sort
[python] class basic methods
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python Basic Course (Introduction)
[Memo] Python3 list sort
Python basic memorandum part 2
python basic on windows ②
Python sort cheat sheet
Python basic memo --Part 2
Python basic course (13 classes)
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Custom sort in Python3
Basic knowledge of Python
[Python] Sort collection types
Python basic grammar memo
OpenCV basic code (python)
Python basic memo --Part 1
python memorandum super basic
Python basic course (8 branches)
Python basic if statement
Natural order in python
Python Basic Course (3 Python Execution)
Python Basic --Pandas, Numpy-
[Python] How to sort dict in list and instance in list
Basic Python 3 grammar (some Python iterations)
Sort Python module imports alphabetically
Python application: Pandas Part 1: Basic
Refactoring Learned in Python (Basic)
Sort by dict type value value
How Python __dict__ is used
Naturally sort Path in Python
[Python] Using OpenCV with Python (Basic)
Python basic course (10 inclusion notation)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Use list-keyed dict in Python
Python Basic Course (5 List Tuples)
Sort huge files with python
python in mongodb in descending sort
Python basic grammar (miscellaneous) Memo (3)