Python Program for Dictionary setdefault() Method

In the previous article, we have discussed Python Program for floor() 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': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}

Output:

The above given dictionary is :
{'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}
The value at the given key hello =  25
The value at the given key btechgeeks=  55
The value at the given key good =  None
The value at the given key morning =  None

Example2:

Input:

Given dictionary = {100: 'good', 150: 'morning', 200: 'btechgeeks'}

Output:

The above given dictionary is :
{100: 'good', 150: 'morning', 200: 'btechgeeks'}
The value at the given key 200 =  btechgeeks
The value at the given key 10 =  None
The value at the given key 150 =  morning

Program for Dictionary setdefault() Method in Python

Dictionary setdefault() Function:

The Python Dictionary setdefault() function is one of several Dictionary functions that can be used to print the value at a specific key position. It prints None if there is no value at the given index.

Syntax:

dictionary.setdefault(keyname, value)

Parameter values:

Keyname: This is required.The keyname of the item from which you want to retrieve the value.

Value: This is optional. This parameter has no effect if the key exists. If the key does not exist, this value takes its place. The default value is None.

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

Approach:

  • Give the dictionary as static input by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary.
  • Apply dictionary.setdefault() method to the above-given dictionary to print the value at a specific key position and print it.
  • It prints None if there is no value at the given index.
  • Similarly, do the same for the other keys and print them.
  • The Exit of Program.

Below is the implementation:

# Give the dictionary by initializing it with some random values and store it
# in a variable.
gvn_dictinry = {'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.setdefault() method to the above-given dictionary to print the
# value at a specific key position and print it.
# It prints None if there is no value at the given index.
print("The value at the given key hello = ",
      gvn_dictinry.setdefault('hello', None))
# similarly do the same for the other keys and print them.
print("The value at the given key btechgeeks= ",
      gvn_dictinry.setdefault('btechgeeks', None))
print("The value at the given key good = ",
      gvn_dictinry.setdefault('good', None))
print("The value at the given key morning = ",
      gvn_dictinry.setdefault('morning', None))

Output:

The above given dictionary is :
{'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}
The value at the given key hello =  25
The value at the given key btechgeeks=  55
The value at the given key good =  None
The value at the given key morning =  None

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.
  • Give the key that you want to fetch the value as user input using the input() function and store it in another variable.
  • Apply dictionary.setdefault() method to the above-given dictionary to print the value at a specific key position and print it.
  • It prints None if there is no value at the given index.
  • Similarly, do the same for the other keys and print them.
  • The Exit of Program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dictinry = {}
# 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_dictinry[keyy] = valuee

# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Give the key that you want to fetch the value as user input using the input()
# function and store it in another variable.
gvn_key1 = input("Enter some random number = ")
# Apply dictionary.setdefault() method to the above-given dictionary to print the
# value at a given specific key position and print it.
# It prints None if there is no value at the given index.
print("The value at the given key", gvn_key1, " = ",
      gvn_dictinry.setdefault(gvn_key1, None))
# similarly do the same for the other keys and print them.
gvn_key2 = input("Enter some random number = ")
print("The value at the given key", gvn_key2, " = ",
      gvn_dictinry.setdefault(gvn_key2, None))

Output:

Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = 20 good
Enter key and value separated by spaces = 40 morning
Enter key and value separated by spaces = 60 btechgeeks
The above given dictionary is :
{'20': 'good', '40': 'morning', '60': 'btechgeeks'}
Enter some random number = 40
The value at the given key 40 = morning
Enter some random number = 100
The value at the given key 100 = None

Learn several Python Dictionary Method Examples on our page and access the dictionary items easily.