# Copy a dictionary, except some keys

**URL:** <https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559>\
**Category:** Ideas\
**Created:** [October 31, 2019, 1:06pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559 "2019-10-31T13:06:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![facundo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/facundo/32/53_2.png) [@facundo](https://discuss.python.org/u/facundo)\
**Post date:** [October 31, 2019, 1:06pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/1 "2019-10-31T13:06:22Z")

</div>

How common is the need of getting a copy of a dict, except some of its keys?

It happens to me quite frequently that I need a similar dict to what I have, but without some of its keys. Of course I can’t just remove the keys, as other parts of the code still uses the full dict.

I end up doing stuff like this:

```auto
new_dict = {k: v for k, v in old_dict.items() if k not in {'key1', 'key2'}}

```

What if we could do just…?

```auto
new_dict = old_dict.copy(avoid_keys=('key1', 'key2'))  

```

Alternatives:

```auto
new_dict = old_dict.copy(avoid=('key1', 'key2'))  
new_dict = old_dict.copy(except=('key1', 'key2'))  

```

Thanks!

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [October 31, 2019, 5:24pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/2 "2019-10-31T17:24:16Z")

</div>

I don’t know if this is common enough to warrant a method on `dict`, but I would also consider `drop`, `ignore`, and `ignore_keys` as argument names.

---

<div class="post-metadata">

**Author:** ![mjpieters](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mjpieters/32/710_2.png) [@mjpieters](https://discuss.python.org/u/mjpieters)\
**Post date:** [October 31, 2019, 5:58pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/3 "2019-10-31T17:58:26Z")

</div>

You can make your dict comprehension more concise by using the `keys()` dictionary view:

```
new_dict = {k: old_dict[k] for k in old_dict.keys() - {'key1', 'key2'}}

```

These act like sets, so you can use set subtraction.

I agree with Brett that I can’t see this being common enough to complicate the `dict.copy()` method.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 31, 2019, 7:59pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/4 "2019-10-31T19:59:40Z")

</div>

The first version of PEP 584 proposed that dicts could support  
the `-` subtraction operator to do precisely this:

```
new = d1 - d2

```

would return a new dict based on a copy of d1 except for the keys of d2.

PEP 584 has currently dropped that operator to concentrate only on dict  
addition, but I expect that somebody will propose dicts support the full  
set API including subtraction.

---

<div class="post-metadata">

**Author:** ![facundo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/facundo/32/53_2.png) [@facundo](https://discuss.python.org/u/facundo)\
**Post date:** [October 31, 2019, 8:08pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/5 "2019-10-31T20:08:17Z")

</div>

> [@mjpieters](#):
>
> You can make your dict comprehension more concise by using the `keys()` dictionary view:
> 
> ```auto
> new_dict = {k: old_dict[k] for k in old_dict.keys() - {'key1', 'key2'}}
> 
> ```

I expect this to be really slower, as you’re accesing the old dict for every key! I’ve run both snippets in `timeit` and this shows a ~33% slowdown.

---

<div class="post-metadata">

**Author:** ![mjpieters](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mjpieters/32/710_2.png) [@mjpieters](https://discuss.python.org/u/mjpieters)\
**Post date:** [November 1, 2019, 10:31am UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/6 "2019-11-01T10:31:44Z")

</div>

> [@facundo](#):
>
> I expect this to be really slower, as you’re accesing the old dict for every key! I’ve run both snippets in `timeit` and this shows a ~33% slowdown.

Of course, yes. I was aiming for conciseness, but it’s not that big an improvement.

---

<div class="post-metadata">

**Author:** ![taleinat](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/taleinat/32/92_2.png) [@taleinat](https://discuss.python.org/u/taleinat)\
**Post date:** [November 2, 2019, 11:38am UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/7 "2019-11-02T11:38:25Z")

</div>

I don’t think this is common enough to warrant adding a method or extending the abilities of an existing method of `dict`.

I can recommend the [funcy](https://funcy.readthedocs.io/) module for good, well-tested implementations of many such basic operations (not only for dicts). For this case, [funcy.omit()](https://funcy.readthedocs.io/en/stable/colls.html#omit) does exactly what you want:

```python
funcy.omit(old_dict, 'key1', 'key2')

```

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [November 2, 2019, 11:42pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/8 "2019-11-02T23:42:06Z")

</div>

How odd.

I would have taken the existence of funcy.omit as evidence that this  
requested feature _is_ common enough to warrant adding a method, rather  
than the opposite.

---

<div class="post-metadata">

**Author:** ![taleinat](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/taleinat/32/92_2.png) [@taleinat](https://discuss.python.org/u/taleinat)\
**Post date:** [November 3, 2019, 8:24am UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/9 "2019-11-03T08:24:45Z")

</div>

> [@steven.daprano](#):
>
> I would have taken the existence of funcy.omit as evidence that this  
> requested feature _is_ common enough to warrant adding a method, rather  
> than the opposite.

This is the way I see it: The `funcy` library has over a hundred such helpers. If we added many or all of them to Python’s built-ins and stdlib, IMO we’d be doing the language a disservice by increasing the number of basics anyone has to learn to _read and maintain_ Python code. On the other hand, for a specific codebase, deciding to use `funcy` consistently can be beneficial; I’ve seen this done with good results.

---

<div class="post-metadata">

**Author:** ![hprodh](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hprodh/32/22529_2.png) [@hprodh](https://discuss.python.org/u/hprodh)\
**Post date:** [October 10, 2024, 8:49am UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/10 "2024-10-10T08:49:55Z")

</div>

I have considered this possibility at multiple occasions, with the slight difference that i wished for the existence of a `subdict(keys)` method (keeping items instead of dropping them). We basically could represent different operations on dicts like difference (the case proposed by OP), intersection (my case), and also union… this could be effectively represented by some operations on sets of keys (respectively `-`, `*`, `+`), but the ideal syntax is unclear as you want to create/modify dicts while operating on sets of keys…

Whatever, there is another fact to take into account : there is no speed up gained from creating a subset of a dict “all-at-once” compared to iteratively creating it. For example, the `update` method is no (significantly?) faster than updating the elements one-by-one. This is linked to the hashmap data structure properties. (Please correct me if I’m wrong.)

Therefore any additional method would be performing under-the-hood the creation of a list of keys and, subsequently, the creation a new dict or the modification of an existing one by iterative addition or removal of items.

---

<div class="post-metadata">

**Author:** ![hprodh](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hprodh/32/22529_2.png) [@hprodh](https://discuss.python.org/u/hprodh)\
**Post date:** [October 11, 2024, 9:18am UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/11 "2024-10-11T09:18:39Z")

</div>

I know i am unearthing this topic, yet I noticed the `fromkeys` method is a static one, and I think it could take an original dict as an argument, and create an output dict with the provided keys associated to values taken from the original one. This could help clean bits of user code without adding methods. It could be used in conjunction with the operations on `set`s of keys. It also fits well with the intuition coming from the name of the method, I think.

---

<div class="post-metadata">

**Author:** ![makukha](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/makukha/32/21764_2.png) [@makukha](https://discuss.python.org/u/makukha)\
**Post date:** [October 14, 2024, 6:48pm UTC](https://discuss.python.org/t/copy-a-dictionary-except-some-keys/2559/12 "2024-10-14T18:48:26Z")

</div>

> [@facundo](#):
>
> How common is the need of getting a copy of a dict, except some of its keys?

For me this is a frequent use case. Also, Pydantic [supports](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump) both `include=` and `exclude=` args for this purpose (and I like the naming).
