By via Discussions on Python.org at 03Jun2022 15:48:

but shouldnâ€™t subtraction return some object that is a subset of the

first argument, for example,

```
{1, 2, 3} - {1}
```

would give,

```
{2, 3}
```

which is a subset of `{1, 2, 3}`

, but for `list() - dict().items()`

,

subtraction returns an object of a new type, shouldnâ€™t it be a subset

of the first argument, which is a `list()`

, and a subset of a `list()`

does not mean anything, as subtraction is not valid on a list, so the

subtraction `list() - dict().items()`

should also be invalid.

Well, no.

The implementors of itemview wanted subtraction to be a meaningful idea.

Since subtraction between lists is not supported (we could argue for

days about what that should mean, if it were supported) they chose to

use a set operation for subtraction. This has the advantage that it can

be efficient, since a set membership test is O(1).

The result of a set operation is naturally a set.

Have a read of this:

https://docs.python.org/3/library/stdtypes.html#dict-views

which explains some of the rationale, and why the views are considered

set-like, and thus the operations return sets.

If you think of subtraction as a function instead of an operator,

consider that many functions return values of a different type from

their operands. For a particular type there will be a suite of functions

which return values in the same domain (same type), but also some

functions which return values in a new domain, or of a different type.

Youâ€™ve got subtraction between a list and a dict item view. These are

different types already. If another list is not a reasonable result (or,

if not unreasonable, just not the *desired* or *preferred* kind of

result), and the operation canâ€™t really return another itemview (because

such a thing is inherently a view of a dict), a â€śsetâ€ť result is useful

and reasonable. Particularly since it can immediately be used with other

sets!

Cheers,

Cameron Simpson cs@cskk.id.au