Accessing dictionary items by position in Python 3.6+ efficiently
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+.
Given they are ordered, it seems strange that no methods exist to retrieve the ith item of a dictionary by insertion order. The only solutions available appear to have O(n) complexity, either:
- Convert to a list via an O(n) process and then use
list.__getitem__
. enumerate
dictionary items in a loop and return the value when the desired index is reached. Again, with O(n) time complexity.
Since getting an item from a list
has O(1) complexity, is there a way to achieve the same complexity with dictionaries? Either with regular dict
or collections.OrderedDict
would work.
If it's not possible, is there a structural reason preventing such a method, or is this just a feature which has not yet been considered / implemented?
python python-3.x dictionary python-internals
 |Â
show 1 more comment
up vote
6
down vote
favorite
I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+.
Given they are ordered, it seems strange that no methods exist to retrieve the ith item of a dictionary by insertion order. The only solutions available appear to have O(n) complexity, either:
- Convert to a list via an O(n) process and then use
list.__getitem__
. enumerate
dictionary items in a loop and return the value when the desired index is reached. Again, with O(n) time complexity.
Since getting an item from a list
has O(1) complexity, is there a way to achieve the same complexity with dictionaries? Either with regular dict
or collections.OrderedDict
would work.
If it's not possible, is there a structural reason preventing such a method, or is this just a feature which has not yet been considered / implemented?
python python-3.x dictionary python-internals
It's implemented as a linked list. Otherwise, it would be impossible to delete elements.
â o11c
3 hours ago
I can think of an obscure reason. It makes JSON-lines more stable without an enclosing list and individual dictionaries. Beyond that very minor detail, I've not really understood the hype
â roganjosh
3 hours ago
@o11c, OK, so there's clearly a gap in my understanding. But I can see (maybe) what you mean, perhaps you need to have O(n) position access to keep O(1) deletion vs O(n) for lists.
â jpp
3 hours ago
2
@o11c it is not implemented as a linked-list
â juanpa.arrivillaga
3 hours ago
1
I think they just don't want to add sequence-like behavior to what is fundamentally a mapping. IOW: you shouldn't be using your dict like a list, but it does maintain order.
â juanpa.arrivillaga
3 hours ago
 |Â
show 1 more comment
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+.
Given they are ordered, it seems strange that no methods exist to retrieve the ith item of a dictionary by insertion order. The only solutions available appear to have O(n) complexity, either:
- Convert to a list via an O(n) process and then use
list.__getitem__
. enumerate
dictionary items in a loop and return the value when the desired index is reached. Again, with O(n) time complexity.
Since getting an item from a list
has O(1) complexity, is there a way to achieve the same complexity with dictionaries? Either with regular dict
or collections.OrderedDict
would work.
If it's not possible, is there a structural reason preventing such a method, or is this just a feature which has not yet been considered / implemented?
python python-3.x dictionary python-internals
I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+.
Given they are ordered, it seems strange that no methods exist to retrieve the ith item of a dictionary by insertion order. The only solutions available appear to have O(n) complexity, either:
- Convert to a list via an O(n) process and then use
list.__getitem__
. enumerate
dictionary items in a loop and return the value when the desired index is reached. Again, with O(n) time complexity.
Since getting an item from a list
has O(1) complexity, is there a way to achieve the same complexity with dictionaries? Either with regular dict
or collections.OrderedDict
would work.
If it's not possible, is there a structural reason preventing such a method, or is this just a feature which has not yet been considered / implemented?
python python-3.x dictionary python-internals
python python-3.x dictionary python-internals
asked 3 hours ago
jpp
66.1k173981
66.1k173981
It's implemented as a linked list. Otherwise, it would be impossible to delete elements.
â o11c
3 hours ago
I can think of an obscure reason. It makes JSON-lines more stable without an enclosing list and individual dictionaries. Beyond that very minor detail, I've not really understood the hype
â roganjosh
3 hours ago
@o11c, OK, so there's clearly a gap in my understanding. But I can see (maybe) what you mean, perhaps you need to have O(n) position access to keep O(1) deletion vs O(n) for lists.
â jpp
3 hours ago
2
@o11c it is not implemented as a linked-list
â juanpa.arrivillaga
3 hours ago
1
I think they just don't want to add sequence-like behavior to what is fundamentally a mapping. IOW: you shouldn't be using your dict like a list, but it does maintain order.
â juanpa.arrivillaga
3 hours ago
 |Â
show 1 more comment
It's implemented as a linked list. Otherwise, it would be impossible to delete elements.
â o11c
3 hours ago
I can think of an obscure reason. It makes JSON-lines more stable without an enclosing list and individual dictionaries. Beyond that very minor detail, I've not really understood the hype
â roganjosh
3 hours ago
@o11c, OK, so there's clearly a gap in my understanding. But I can see (maybe) what you mean, perhaps you need to have O(n) position access to keep O(1) deletion vs O(n) for lists.
â jpp
3 hours ago
2
@o11c it is not implemented as a linked-list
â juanpa.arrivillaga
3 hours ago
1
I think they just don't want to add sequence-like behavior to what is fundamentally a mapping. IOW: you shouldn't be using your dict like a list, but it does maintain order.
â juanpa.arrivillaga
3 hours ago
It's implemented as a linked list. Otherwise, it would be impossible to delete elements.
â o11c
3 hours ago
It's implemented as a linked list. Otherwise, it would be impossible to delete elements.
â o11c
3 hours ago
I can think of an obscure reason. It makes JSON-lines more stable without an enclosing list and individual dictionaries. Beyond that very minor detail, I've not really understood the hype
â roganjosh
3 hours ago
I can think of an obscure reason. It makes JSON-lines more stable without an enclosing list and individual dictionaries. Beyond that very minor detail, I've not really understood the hype
â roganjosh
3 hours ago
@o11c, OK, so there's clearly a gap in my understanding. But I can see (maybe) what you mean, perhaps you need to have O(n) position access to keep O(1) deletion vs O(n) for lists.
â jpp
3 hours ago
@o11c, OK, so there's clearly a gap in my understanding. But I can see (maybe) what you mean, perhaps you need to have O(n) position access to keep O(1) deletion vs O(n) for lists.
â jpp
3 hours ago
2
2
@o11c it is not implemented as a linked-list
â juanpa.arrivillaga
3 hours ago
@o11c it is not implemented as a linked-list
â juanpa.arrivillaga
3 hours ago
1
1
I think they just don't want to add sequence-like behavior to what is fundamentally a mapping. IOW: you shouldn't be using your dict like a list, but it does maintain order.
â juanpa.arrivillaga
3 hours ago
I think they just don't want to add sequence-like behavior to what is fundamentally a mapping. IOW: you shouldn't be using your dict like a list, but it does maintain order.
â juanpa.arrivillaga
3 hours ago
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
For an OrderedDict
it's inherently O(n)
because the ordering is recorded in a linked list.
For the builtin dict, there's a vector (a contiguous array) rather than a linked list, but pretty much the same thing in the end: the vector contains a few kind of "dummies", special internal values that mean "no key has been stored here yet" or "a key used to be stored here but no longer". That makes, e.g., deleting a key extremely cheap (just overwrite the key with a dummy value).
But without adding auxiliary data structures on top of that, there's no way to skip over the dummies without marching over them one at a time. Because Python uses a form of open addressing for collision resolution, and keeps the load factor under 2/3, at least a third of the vector's entries are dummies. the_vector[i]
can be accessed in O(1)
time, but really has no predictable relation to the i'th non-dummy entry.
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
4
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c'sdictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.
â Tim Peters
3 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
For an OrderedDict
it's inherently O(n)
because the ordering is recorded in a linked list.
For the builtin dict, there's a vector (a contiguous array) rather than a linked list, but pretty much the same thing in the end: the vector contains a few kind of "dummies", special internal values that mean "no key has been stored here yet" or "a key used to be stored here but no longer". That makes, e.g., deleting a key extremely cheap (just overwrite the key with a dummy value).
But without adding auxiliary data structures on top of that, there's no way to skip over the dummies without marching over them one at a time. Because Python uses a form of open addressing for collision resolution, and keeps the load factor under 2/3, at least a third of the vector's entries are dummies. the_vector[i]
can be accessed in O(1)
time, but really has no predictable relation to the i'th non-dummy entry.
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
4
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c'sdictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.
â Tim Peters
3 hours ago
add a comment |Â
up vote
8
down vote
accepted
For an OrderedDict
it's inherently O(n)
because the ordering is recorded in a linked list.
For the builtin dict, there's a vector (a contiguous array) rather than a linked list, but pretty much the same thing in the end: the vector contains a few kind of "dummies", special internal values that mean "no key has been stored here yet" or "a key used to be stored here but no longer". That makes, e.g., deleting a key extremely cheap (just overwrite the key with a dummy value).
But without adding auxiliary data structures on top of that, there's no way to skip over the dummies without marching over them one at a time. Because Python uses a form of open addressing for collision resolution, and keeps the load factor under 2/3, at least a third of the vector's entries are dummies. the_vector[i]
can be accessed in O(1)
time, but really has no predictable relation to the i'th non-dummy entry.
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
4
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c'sdictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.
â Tim Peters
3 hours ago
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
For an OrderedDict
it's inherently O(n)
because the ordering is recorded in a linked list.
For the builtin dict, there's a vector (a contiguous array) rather than a linked list, but pretty much the same thing in the end: the vector contains a few kind of "dummies", special internal values that mean "no key has been stored here yet" or "a key used to be stored here but no longer". That makes, e.g., deleting a key extremely cheap (just overwrite the key with a dummy value).
But without adding auxiliary data structures on top of that, there's no way to skip over the dummies without marching over them one at a time. Because Python uses a form of open addressing for collision resolution, and keeps the load factor under 2/3, at least a third of the vector's entries are dummies. the_vector[i]
can be accessed in O(1)
time, but really has no predictable relation to the i'th non-dummy entry.
For an OrderedDict
it's inherently O(n)
because the ordering is recorded in a linked list.
For the builtin dict, there's a vector (a contiguous array) rather than a linked list, but pretty much the same thing in the end: the vector contains a few kind of "dummies", special internal values that mean "no key has been stored here yet" or "a key used to be stored here but no longer". That makes, e.g., deleting a key extremely cheap (just overwrite the key with a dummy value).
But without adding auxiliary data structures on top of that, there's no way to skip over the dummies without marching over them one at a time. Because Python uses a form of open addressing for collision resolution, and keeps the load factor under 2/3, at least a third of the vector's entries are dummies. the_vector[i]
can be accessed in O(1)
time, but really has no predictable relation to the i'th non-dummy entry.
answered 3 hours ago
Tim Peters
40.3k66890
40.3k66890
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
4
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c'sdictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.
â Tim Peters
3 hours ago
add a comment |Â
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
4
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c'sdictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.
â Tim Peters
3 hours ago
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
From my understanding of the >3.6 implementation, there are two vectors, the sparse-index array is where the open-addressing occurs, but that the actual entries vector is simply that, an array of the entries in order, with no dummies, no?
â juanpa.arrivillaga
3 hours ago
4
4
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c's
dictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.â Tim Peters
3 hours ago
@juanpa.arrivillaga, it's more complicated - what isn't? ;-) There are "split" and "non-split" dicts under the covers, etc. For a regular old dict ("non-split"), deleting a key also sets the corresponding value slot to NULL, so same thing; you have to skip over the NULL values one at a time. See the loop in dictobject.c's
dictiter_iternextkey()
. Iterating over "the keys" actually iterates over the values, which are in insertion order, but can contain NULLs in arbitrary places. Once a none-NULL value is found, it contains a pointer back to the key.â Tim Peters
3 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52507860%2faccessing-dictionary-items-by-position-in-python-3-6-efficiently%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It's implemented as a linked list. Otherwise, it would be impossible to delete elements.
â o11c
3 hours ago
I can think of an obscure reason. It makes JSON-lines more stable without an enclosing list and individual dictionaries. Beyond that very minor detail, I've not really understood the hype
â roganjosh
3 hours ago
@o11c, OK, so there's clearly a gap in my understanding. But I can see (maybe) what you mean, perhaps you need to have O(n) position access to keep O(1) deletion vs O(n) for lists.
â jpp
3 hours ago
2
@o11c it is not implemented as a linked-list
â juanpa.arrivillaga
3 hours ago
1
I think they just don't want to add sequence-like behavior to what is fundamentally a mapping. IOW: you shouldn't be using your dict like a list, but it does maintain order.
â juanpa.arrivillaga
3 hours ago