Python Program for Dictionary clear() Function

Dictionary in python :

A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.

Examples:

Example1:

Input:

Given dictionary = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}

Output:

The given dictionary before clearing is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The length of the given dictionary before clearing =  4
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing =  0

Example2:

Input:

Given dictionary = {'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}

Output:

The given dictionary before clearing is :
{'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}
The length of the given dictionary before clearing = 5
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing = 0

Program for Dictionary clear() Function in Python

Dictionary clear() Function:

clear() is a Python dictionary method that removes all items from the dictionary.

Syntax:

dict.clear()

Parameters: NA

Return Value: This method has no return value.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the dictionary by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary before clearing.
  • Calculate the length of the given dictionary using the len() function and print it.
  • Apply clear() function to the given dictionary.
  • Print the above-given dictionary after clearing it.
  • Calculate the length of the above dictionary after clearing using the len() function and print it.
  • The Exit of the program.

Below is the implementation:

# Give the dictionary by initializing it with some random values and store it
# in a variable.
gvn_dictinry = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
# Print the above-given dictionary before clearing.
print("The given dictionary before clearing is :")
# Calculate the length of the given dictionary using the len() function and print it.
print(gvn_dictinry)
print("The length of the given dictionary before clearing = ", len(gvn_dictinry))
# Apply clear() function to the given dictionary.
gvn_dictinry.clear()
# Print the above-given dictionary after clearing it.
print("The given dictionary After clearing is :")
print(gvn_dictinry)
# Calculate the length of the above dictionary after clearing using the len() function
# and print it.
print("The length of the given dictionary After clearing = ", len(gvn_dictinry))

Output:

The given dictionary before clearing is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The length of the given dictionary before clearing =  4
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing =  0

Method #2: Using Built-in Functions (User Input)

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Print the above-given dictionary before clearing.
  • Calculate the length of the given dictionary using the len() function and print it.
  • Apply clear() function to the given dictionary.
  • Print the above-given dictionary after clearing it.
  • Calculate the length of the above dictionary after clearing using the len() function and print it.
  • The Exit of the program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee =  input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dict[keyy] = valuee

# Print the above-given dictionary before clearing.
print("The given dictionary before clearing is :")
# Calculate the length of the given dictionary using the len() function and print it.
print(gvn_dict)
print("The length of the given dictionary before clearing = ", len(gvn_dict))
# Apply clear() function to the given dictionary.
gvn_dict.clear()
# Print the above-given dictionary after clearing it.
print("The given dictionary After clearing is :")
print(gvn_dict)
# Calculate the length of the above dictionary after clearing using the len() function
# and print it.
print("The length of the given dictionary After clearing = ", len(gvn_dict))

Output:

Enter some random number of keys of the dictionary = 5
Enter key and value separated by spaces = good 1
Enter key and value separated by spaces = morning 2
Enter key and value separated by spaces = this 3
Enter key and value separated by spaces = is 4
Enter key and value separated by spaces = btechgeeks 5
The given dictionary before clearing is :
{'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}
The length of the given dictionary before clearing = 5
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing = 0