Two ways to clear a dictionary in Python

Directly through the following two examples:

  1. Use {} to clear dictionary value
>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> x
{'key': 'value'}
>>> y
{'key': 'value'}
>>> x = {}
>>> x
{}
>>> y
{'key': 'value'}
>>> 
  1. Use clear () to clear the dictionary value
>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> x
{'key': 'value'}
>>> y
{'key': 'value'}
>>> x.clear()
>>> x
{}
>>> y
{}

Different methods can be used according to requirements.

Published 89 original articles · Liked 83 · Visits 3490

Guess you like

Origin blog.csdn.net/devin_xin/article/details/105410601