Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Why is dictionary.keys() a list and not a set?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Alex Martelli, post: 1850593"] Absolutely not in my use cases. The typical reason I'm asking for ..values() is because each value is a count (e.g. of how many time the key has occurred) and I want the total, so the typical idiom is sum(d.values()) -- getting a result set would be an utter disaster, and should I ever want it, set(d.values()) is absolutely trivial to code. Note that in both cases I don't need a LIST, just an ITERATOR, so itervalues would do just as well (and probably faster) except it looks more cluttered and by a tiny margin less readable -- "the sum of the values" rolls off the tongue, "the sum of the itervalues" doesn't!-) So, the direction of change for Python 3000, when backwards compatibility can be broken, is to return iterators rather than lists. At that time, should you ever want a list, you'll say so explicitly, as you do now when you want a set, i.e. list(d.values()) Historically, the 'in' operator was added to dictionaries (and the special method __contains__ introduced, to let you support containment checking in your own types) well before sets were added to Python (first as a standard library module, only later as a built-in). In Python today 'in' doesn't necessarily mean set membership, but some fuzzier notion of "presence in container"; e..g., you can code 'zap' in 'bazapper' and get the result True (while 'paz' in 'bazapper' would be False, even though, if you thought of the strings as SETS rather than SEQUENCES of characters, that would be absurd). So far, strings (plain and unicode) are the only containers that read 'in' this way (presence of subsequence rather than of single item), but one example should be enough to show that "set membership" isn't exactly what the 'in' operator means. You appear to have a strange notion of "derived data type". In what sense, for example, would a list BE-A set? It breaks all kind of class invariants, e.g. "number of items is identical to number of distinct items", commutativity of addition, etc.. Alex [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Why is dictionary.keys() a list and not a set?
Top