Issue
dict
methodsdict.keys()
,dict.items()
anddict.values()
return “views” instead of lists.
First of all, how is a view different from an iterator? Secondly, what is the benefit of this change? Is it just for performance reasons?
It doesn't seem intuitive to me, i.e., I'm asking for a list of things (give me all your keys) and I'm getting something else back. Will this confuse people?
Solution
You are effectively getting a list. It's just not a copy of the internal list, but something that acts as if it where a list but only represents the internal state.
That's the same way it's implemented in Java (and probably many other languages/environments as well).
The main reason is that for many use cases returning a completely detached list is unnecessary and wasteful. It would require copying the entire content (which may or many not be a lot).
If you simply want to iterate over the keys then creating a new list is not necessary. And if you indeed need it as a separate list (as a copy) then you can easily create that list from the view.
Answered By - Joachim Sauer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.