17 of 151 menu

The clear method

The clear method removes all elements from the dictionary. We do not specify anything in the method parameter.

Syntax

dictionary.clear()

Example

Let's remove all the elements in our dictionary:

dct = { 'a': 1, 'b': 2, 'c': 3 } dct.clear() print(dct)

Result of code execution:

{}

See also

  • function len,
    which returns the length of the dictionary
  • method pop,
    which removes an element by its key
  • method popitem,
    which deletes a key-value pair
  • method copy,
    which copies a dictionary
byenru