
On 5/29/07, Josiah Carlson <jcarlson@uci.edu> wrote:
Ron Adam <rrr@ronadam.com> wrote:
Josiah Carlson wrote:
dict.fromkeys() is not seen as being a design mistake
ermm.... I think at least a few see it as something of a leftover, for using dicts as sets. The docs also (weakly) support this view. This isn't quite the only use case, but I doubt fromkeys would be added today (given both sets and subclasses of defaultdict); there just isn't quite enough discomfort to remove it.
splitdict(d, keys): """ Split dictionary d using keys. """ keys_rest = set(d.keys()) - set(keys) return d.from_keys(keys), d.from_keys(keys_rest)
I can't think of a simple one-liner for this one that wouldn't duplicate work.
*this* is the core of a useful idea. list (and set and generator) comprehensions can't partition very well, because they have only a single output. There isn't a good way to say: list_a = [x for x in src if pred(a)] src = [x for x in src if not pred(a)] list_b = [x for x in src if pred(b)] src = [x for x in src if not pred(b)] list_c = [x for x in src if pred(c)] list_other = [x for x in src if not pred(c)] On the other hand, you can do it (inefficiently) as above, or you can write an (ugly) version using a custom function, so the solution would have to be pretty good before it justified complicating the comprehension APIs.
unlikely to happen precisely because *others* aren't mentioning, "dictionaries need to be cleaned up".
Not in so many words; Raymond is very reluctant to add anything, because the API is already fairly large. Guido's ABC for mappings (http://svn.python.org/view/sandbox/trunk/abc/) is explicitly a small subset of what dict offers (and doesn't include fromkeys). That said, saying "too large, this isn't needed" is still a far cry from "so we'll remove it", let alone "and add this stuff to replace it". -jJ