Home Coding Dictionary – CLEAR

Dictionary – CLEAR

0
Dictionary – CLEAR

 
 
# Continue our work with dictionaries
# Clear a dictionary, delete all key:value pairs
#  CLEAR


# Just create two dictionaries.
my_dict = {"a":"Peter","b":"Anna","c":"Mark", "d":"Jeff", "e":"Grace"}
my_dict1 = {"1":"One","2":"Two","3":"Three", "4":"Four"}

# Create a copy of each dictionary, create other 2 dictionaries
# Using .clear() we can remove all key:value from my_dict
my_dict2 = my_dict
my_dict3 = my_dict1

# If we use .clear() methods on the my_dict2, copy of My_dict
# also, my_dict will be erased
my_dict2.clear()

# If we reassign at my_dict3 with an empty dictionary {} 
# only my_dict3 will be affected, and my_dict1 will remain populated
my_dict3 = {}

# copy www.AEC.codes
OUT = my_dict, my_dict2, my_dict1, my_dict3
 
# NOTE
# Be aware of using .clear() methods because it will clear 
# all the references to that dictionary




Syntax

The syntax of clear() is:
dictionary.clear()

Parameter values

This method doesn’t take any parameters

Returns

This method doesn’t return any value (returns None).