use set notation for repr of dict_keys?

Marco Sulla Marco.Sulla.Python at gmail.com
Wed Feb 24 15:10:51 EST 2021


On Wed, 24 Feb 2021 at 15:02, Random832 <random832 at fastmail.com> wrote:
> On Wed, Feb 24, 2021, at 02:59, Marco Sulla wrote:
> > On Wed, 24 Feb 2021 at 06:29, Random832 <random832 at fastmail.com> wrote:
> > > I was surprised, though, to find that you can't remove items directly from the key set, or in general update it in place with &= or -= (these operators work, but give a new set object).
> >
> > This is because they are a view. Changing the key object means you
> > will change the underlying dict. Probably not that you want or expect.
>
> Why wouldn't it be what I want or expect? Java allows exactly this

I didn't know this. I like Java, but IMHO it's quite confusing that
you can remove a key from a Map using the keys object. In my mind it's
more natural to think views as read-only, while changes can be done
only using the original object. But maybe my mind has too strict
bounds.

> [and it's the only way provided to, for example, remove all keys matching a
> predicate in a single pass... an operation that Python sets don't support either]

I hope indeed that someday Python can do:

filtered_dict = a_dict - a_set


More information about the Python-list mailing list