why dict_items object does not support indexing
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I know that you can cast dict_items
into a list
to allow item indexing. But I do not know why this operation is not allowed directly. Is it because dict_items
objects are generators? If so, when I'm seeing
>>> "foo": "bar", "baz": "qux".items()
dict_items([('foo', 'bar'), ('baz', 'qux')])
is python evaluating my generator when repr
is called?
python python-3.x
add a comment |Â
up vote
6
down vote
favorite
I know that you can cast dict_items
into a list
to allow item indexing. But I do not know why this operation is not allowed directly. Is it because dict_items
objects are generators? If so, when I'm seeing
>>> "foo": "bar", "baz": "qux".items()
dict_items([('foo', 'bar'), ('baz', 'qux')])
is python evaluating my generator when repr
is called?
python python-3.x
2
Note that you're not casting it to alist
; you're creating a new list from it. Whether or not this is efficient depends on the Python implementation.
â Jonathon Reinhart
3 hours ago
There is no built-in nameddict_items
. I assume you mean thedict
methoditems()
.
â martineau
3 hours ago
3
@martineau:type(.items()).__name__
->'dict_items'
â Mad Physicist
3 hours ago
1
I think it's because the object of typedict_items
that thedict.items()
method returns is a dictionary view object which are dynamic and reflect changes that may be occurring to the dictionary on-the-flyâÂÂso indexing something like that which might change at any moment doesn't make too much sense. If you convert it to a list, the contents of that list would be changed by updates to the dictionary it came from.
â martineau
3 hours ago
Related: Accessing dictionary items by position in Python 3.6+ efficiently, specifically this answer.
â jpp
2 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I know that you can cast dict_items
into a list
to allow item indexing. But I do not know why this operation is not allowed directly. Is it because dict_items
objects are generators? If so, when I'm seeing
>>> "foo": "bar", "baz": "qux".items()
dict_items([('foo', 'bar'), ('baz', 'qux')])
is python evaluating my generator when repr
is called?
python python-3.x
I know that you can cast dict_items
into a list
to allow item indexing. But I do not know why this operation is not allowed directly. Is it because dict_items
objects are generators? If so, when I'm seeing
>>> "foo": "bar", "baz": "qux".items()
dict_items([('foo', 'bar'), ('baz', 'qux')])
is python evaluating my generator when repr
is called?
python python-3.x
python python-3.x
edited 3 hours ago
Mad Physicist
30.7k156389
30.7k156389
asked 3 hours ago
ohe
1,80911835
1,80911835
2
Note that you're not casting it to alist
; you're creating a new list from it. Whether or not this is efficient depends on the Python implementation.
â Jonathon Reinhart
3 hours ago
There is no built-in nameddict_items
. I assume you mean thedict
methoditems()
.
â martineau
3 hours ago
3
@martineau:type(.items()).__name__
->'dict_items'
â Mad Physicist
3 hours ago
1
I think it's because the object of typedict_items
that thedict.items()
method returns is a dictionary view object which are dynamic and reflect changes that may be occurring to the dictionary on-the-flyâÂÂso indexing something like that which might change at any moment doesn't make too much sense. If you convert it to a list, the contents of that list would be changed by updates to the dictionary it came from.
â martineau
3 hours ago
Related: Accessing dictionary items by position in Python 3.6+ efficiently, specifically this answer.
â jpp
2 hours ago
add a comment |Â
2
Note that you're not casting it to alist
; you're creating a new list from it. Whether or not this is efficient depends on the Python implementation.
â Jonathon Reinhart
3 hours ago
There is no built-in nameddict_items
. I assume you mean thedict
methoditems()
.
â martineau
3 hours ago
3
@martineau:type(.items()).__name__
->'dict_items'
â Mad Physicist
3 hours ago
1
I think it's because the object of typedict_items
that thedict.items()
method returns is a dictionary view object which are dynamic and reflect changes that may be occurring to the dictionary on-the-flyâÂÂso indexing something like that which might change at any moment doesn't make too much sense. If you convert it to a list, the contents of that list would be changed by updates to the dictionary it came from.
â martineau
3 hours ago
Related: Accessing dictionary items by position in Python 3.6+ efficiently, specifically this answer.
â jpp
2 hours ago
2
2
Note that you're not casting it to a
list
; you're creating a new list from it. Whether or not this is efficient depends on the Python implementation.â Jonathon Reinhart
3 hours ago
Note that you're not casting it to a
list
; you're creating a new list from it. Whether or not this is efficient depends on the Python implementation.â Jonathon Reinhart
3 hours ago
There is no built-in named
dict_items
. I assume you mean the dict
method items()
.â martineau
3 hours ago
There is no built-in named
dict_items
. I assume you mean the dict
method items()
.â martineau
3 hours ago
3
3
@martineau:
type(.items()).__name__
-> 'dict_items'
â Mad Physicist
3 hours ago
@martineau:
type(.items()).__name__
-> 'dict_items'
â Mad Physicist
3 hours ago
1
1
I think it's because the object of type
dict_items
that the dict.items()
method returns is a dictionary view object which are dynamic and reflect changes that may be occurring to the dictionary on-the-flyâÂÂso indexing something like that which might change at any moment doesn't make too much sense. If you convert it to a list, the contents of that list would be changed by updates to the dictionary it came from.â martineau
3 hours ago
I think it's because the object of type
dict_items
that the dict.items()
method returns is a dictionary view object which are dynamic and reflect changes that may be occurring to the dictionary on-the-flyâÂÂso indexing something like that which might change at any moment doesn't make too much sense. If you convert it to a list, the contents of that list would be changed by updates to the dictionary it came from.â martineau
3 hours ago
Related: Accessing dictionary items by position in Python 3.6+ efficiently, specifically this answer.
â jpp
2 hours ago
Related: Accessing dictionary items by position in Python 3.6+ efficiently, specifically this answer.
â jpp
2 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
dict_items
do not support indexing because these objects are intended to be set-like, and sets do not support indexing.
They do quack like sets in other ways:
>>> d1 = 'k1': 'v1', 'k2': 'v2', 'k3': 'v3'
>>> d2 = 'k2': 'v2', 'k3': 'not v3'
>>> d1.items() & d2.items()
('k2', 'v2')
>>> d1.items() | d2.items()
('k1', 'v1'), ('k2', 'v2'), ('k3', 'not v3'), ('k3', 'v3')
If any value is not hashable, you lose the ability to treat the dict items views with set operations.
It is not sensible to give indexing support to dict_items
views, because the dict does not have an ordering until Python 3.7+, so accessing "the 0th" item would not be well-defined. Even in Python 3.7, where there is a sensible ordering to use for indexing (i.e. the insertion order), it is non-trivial to implement this with O(1) complexity, so it's not supported. The "unwritten rule" is that indexing should be constant-time operation (as it is for list, tuple, dict, str - see here).
2
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
1
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
1
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
2
Good answer. To appreciateit is non-trivial to implement this with O(1) complexity
, see this answer and the comments.
â jpp
2 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
7
down vote
dict_items
do not support indexing because these objects are intended to be set-like, and sets do not support indexing.
They do quack like sets in other ways:
>>> d1 = 'k1': 'v1', 'k2': 'v2', 'k3': 'v3'
>>> d2 = 'k2': 'v2', 'k3': 'not v3'
>>> d1.items() & d2.items()
('k2', 'v2')
>>> d1.items() | d2.items()
('k1', 'v1'), ('k2', 'v2'), ('k3', 'not v3'), ('k3', 'v3')
If any value is not hashable, you lose the ability to treat the dict items views with set operations.
It is not sensible to give indexing support to dict_items
views, because the dict does not have an ordering until Python 3.7+, so accessing "the 0th" item would not be well-defined. Even in Python 3.7, where there is a sensible ordering to use for indexing (i.e. the insertion order), it is non-trivial to implement this with O(1) complexity, so it's not supported. The "unwritten rule" is that indexing should be constant-time operation (as it is for list, tuple, dict, str - see here).
2
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
1
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
1
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
2
Good answer. To appreciateit is non-trivial to implement this with O(1) complexity
, see this answer and the comments.
â jpp
2 hours ago
add a comment |Â
up vote
7
down vote
dict_items
do not support indexing because these objects are intended to be set-like, and sets do not support indexing.
They do quack like sets in other ways:
>>> d1 = 'k1': 'v1', 'k2': 'v2', 'k3': 'v3'
>>> d2 = 'k2': 'v2', 'k3': 'not v3'
>>> d1.items() & d2.items()
('k2', 'v2')
>>> d1.items() | d2.items()
('k1', 'v1'), ('k2', 'v2'), ('k3', 'not v3'), ('k3', 'v3')
If any value is not hashable, you lose the ability to treat the dict items views with set operations.
It is not sensible to give indexing support to dict_items
views, because the dict does not have an ordering until Python 3.7+, so accessing "the 0th" item would not be well-defined. Even in Python 3.7, where there is a sensible ordering to use for indexing (i.e. the insertion order), it is non-trivial to implement this with O(1) complexity, so it's not supported. The "unwritten rule" is that indexing should be constant-time operation (as it is for list, tuple, dict, str - see here).
2
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
1
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
1
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
2
Good answer. To appreciateit is non-trivial to implement this with O(1) complexity
, see this answer and the comments.
â jpp
2 hours ago
add a comment |Â
up vote
7
down vote
up vote
7
down vote
dict_items
do not support indexing because these objects are intended to be set-like, and sets do not support indexing.
They do quack like sets in other ways:
>>> d1 = 'k1': 'v1', 'k2': 'v2', 'k3': 'v3'
>>> d2 = 'k2': 'v2', 'k3': 'not v3'
>>> d1.items() & d2.items()
('k2', 'v2')
>>> d1.items() | d2.items()
('k1', 'v1'), ('k2', 'v2'), ('k3', 'not v3'), ('k3', 'v3')
If any value is not hashable, you lose the ability to treat the dict items views with set operations.
It is not sensible to give indexing support to dict_items
views, because the dict does not have an ordering until Python 3.7+, so accessing "the 0th" item would not be well-defined. Even in Python 3.7, where there is a sensible ordering to use for indexing (i.e. the insertion order), it is non-trivial to implement this with O(1) complexity, so it's not supported. The "unwritten rule" is that indexing should be constant-time operation (as it is for list, tuple, dict, str - see here).
dict_items
do not support indexing because these objects are intended to be set-like, and sets do not support indexing.
They do quack like sets in other ways:
>>> d1 = 'k1': 'v1', 'k2': 'v2', 'k3': 'v3'
>>> d2 = 'k2': 'v2', 'k3': 'not v3'
>>> d1.items() & d2.items()
('k2', 'v2')
>>> d1.items() | d2.items()
('k1', 'v1'), ('k2', 'v2'), ('k3', 'not v3'), ('k3', 'v3')
If any value is not hashable, you lose the ability to treat the dict items views with set operations.
It is not sensible to give indexing support to dict_items
views, because the dict does not have an ordering until Python 3.7+, so accessing "the 0th" item would not be well-defined. Even in Python 3.7, where there is a sensible ordering to use for indexing (i.e. the insertion order), it is non-trivial to implement this with O(1) complexity, so it's not supported. The "unwritten rule" is that indexing should be constant-time operation (as it is for list, tuple, dict, str - see here).
edited 2 hours ago
user2357112
144k12147229
144k12147229
answered 3 hours ago
wim
150k45282409
150k45282409
2
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
1
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
1
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
2
Good answer. To appreciateit is non-trivial to implement this with O(1) complexity
, see this answer and the comments.
â jpp
2 hours ago
add a comment |Â
2
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
1
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
1
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
2
Good answer. To appreciateit is non-trivial to implement this with O(1) complexity
, see this answer and the comments.
â jpp
2 hours ago
2
2
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
That's actually a much better reason than the one I gave of "It's not supported because there's no support for it" :)
â Mad Physicist
3 hours ago
1
1
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
wim: How did you learn all this about the undocumented type? Just by experiments or perhaps reading the CPython source code?
â martineau
3 hours ago
1
1
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
Good question..reading the mailing lists, seeing the comments from CPython core developers on bugs.python.org, finding bugs in the implementation etc.
â wim
3 hours ago
2
2
Good answer. To appreciate
it is non-trivial to implement this with O(1) complexity
, see this answer and the comments.â jpp
2 hours ago
Good answer. To appreciate
it is non-trivial to implement this with O(1) complexity
, see this answer and the comments.â jpp
2 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%2f52900328%2fwhy-dict-items-object-does-not-support-indexing%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
2
Note that you're not casting it to a
list
; you're creating a new list from it. Whether or not this is efficient depends on the Python implementation.â Jonathon Reinhart
3 hours ago
There is no built-in named
dict_items
. I assume you mean thedict
methoditems()
.â martineau
3 hours ago
3
@martineau:
type(.items()).__name__
->'dict_items'
â Mad Physicist
3 hours ago
1
I think it's because the object of type
dict_items
that thedict.items()
method returns is a dictionary view object which are dynamic and reflect changes that may be occurring to the dictionary on-the-flyâÂÂso indexing something like that which might change at any moment doesn't make too much sense. If you convert it to a list, the contents of that list would be changed by updates to the dictionary it came from.â martineau
3 hours ago
Related: Accessing dictionary items by position in Python 3.6+ efficiently, specifically this answer.
â jpp
2 hours ago