How do I erase the elements of Dict instead of pop?

Asked 2 years ago, Updated 2 years ago, 190 views

You are about to erase the element of dict from Python. I know how to erase it with dict.pop() pop() returns elements that are deleted, right?

I'd like to get the disc with the elements removed back.

The function I'm using right now is myremove (original, result, key) I'm getting two dicts as a factor.

Result = myremove(original, key) Is there a way to make it more Python?

python dictionary pop remove

2022-09-22 22:29

1 Answers

del[key] also clears the element. However, deld Since the element d is removed from the original dict, please write the following when you receive a new dict while preserving the new original dict.

def removekey(d, key):
    r = copy d to dict(d) #r
    Remove key from del [key] #r (original d is preserved)
    Dict return with the key removed

The dict() constructor written in this function proceeds a shallow copy. To make a deep copy


2022-09-22 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.