Equivalent for LinkedHashMap in Python
Question:
LinkedHashMap is the Java implementation of a Hashtable like data structure (dict in Python) with predictable iteration order. That means that during a traversal over all keys, they are ordered by insertion. This is done by an additional linked list maintaining the insertion order.
Is there an equivalent to that in Python?
Answers:
Although you can do the same thing by maintaining a list to keep track of insertion order, Python 2.7 and Python >=3.1 have an OrderedDict class in the collections module.
Before 2.7, you can subclass dict
following this recipe.
I don’t think so; you’d have to use a dict plus a list. But you could pretty easily wrap that in a class, and define keys
, __getitem__
, __setitem__
, etc. to make it work the way you want.
I am not sure whether this is what you are asking for:
>>> dic = {1: 'one', 2: 'two'}
>>> for k, v in dic.iteritems():
... print k, v
you can order the dic in the order of the insertion using ordereddict module.
d = ordereddict(dic, relax=True)
As of Python 3.7, dict
objects maintain their insertion order by default.
If you’re on Python 2.7 or Python >=3.1 you can use collections.OrderedDict in the standard library.
This answer to the question How do you retrieve items from a dictionary in the order that they’re inserted? contains an implementation of an ordered dict, in case you’re not using Python 3.x and don’t want to give yourself a dependency on the third-party ordereddict module.
In addition to the validated comment;
As of Python 3.7, dict
preserves insertion order.
This answer shows this with details.
LinkedHashMap is the Java implementation of a Hashtable like data structure (dict in Python) with predictable iteration order. That means that during a traversal over all keys, they are ordered by insertion. This is done by an additional linked list maintaining the insertion order.
Is there an equivalent to that in Python?
Although you can do the same thing by maintaining a list to keep track of insertion order, Python 2.7 and Python >=3.1 have an OrderedDict class in the collections module.
Before 2.7, you can subclass dict
following this recipe.
I don’t think so; you’d have to use a dict plus a list. But you could pretty easily wrap that in a class, and define keys
, __getitem__
, __setitem__
, etc. to make it work the way you want.
I am not sure whether this is what you are asking for:
>>> dic = {1: 'one', 2: 'two'}
>>> for k, v in dic.iteritems():
... print k, v
you can order the dic in the order of the insertion using ordereddict module.
d = ordereddict(dic, relax=True)
As of Python 3.7, dict
objects maintain their insertion order by default.
If you’re on Python 2.7 or Python >=3.1 you can use collections.OrderedDict in the standard library.
This answer to the question How do you retrieve items from a dictionary in the order that they’re inserted? contains an implementation of an ordered dict, in case you’re not using Python 3.x and don’t want to give yourself a dependency on the third-party ordereddict module.
In addition to the validated comment;
As of Python 3.7, dict
preserves insertion order.
This answer shows this with details.