Two methods for clearing the dictionary in python: clear indicates clearing the dictionary content in python. it is different from deleting the dictionary. comparison between the two methods for clearing the dictionary in python
Clear refers to clearing the dictionary content in python, which is different from deleting the dictionary.
For example:
D = {'name': 1, 'age': 2}
There are two methods to clear the dictionary.
Method 1:
D. clear ()
Print the value of d {}
Method 2:
D = {}
Print the value of d {}
Differences between the two:
>>> D = {'name': 1, 'age': 2}
>>> X = d
>>> X
{'Age': 2, 'name': 1}
>>> X = {}
>>> X
{}
>>> D
{'Age': 2, 'name': 1}
>>>
We can see that when x = {} is used, the value of x itself is cleared, that is, the reference of x to d is changed, and the value of d is not changed.
>>> D = {'namge': 1, 'age': 2}
>>> X = d
>>> X
{'Namge': 1, 'age': 2}
>>> D
{'Namge': 1, 'age': 2}
>>> X. clear ()
>>> X
{}
>>> D
{}
>>>
When X = clear (), the value in the reference is cleared. when x = clear () is used, the value of d also changes, the value of D does not change when x = {} is used.