Program for Dictionary items() Function

Python Program for Dictionary items() 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 above given dictionary is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The above given dictionary items are :
dict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

Example2:

Input:

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

Output:

The above given dictionary is :
{'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}
The above given dictionary items are :
dict_items([('good', '1'), ('morning', '3'), ('this', '5'), ('is', '7'), ('btechgeeks', '9')])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

Program for Dictionary items() Function in Python

Dictionary items() Function:

Python’s items Dictionary function returns a list of dictionary items that are currently available (total keys and values).

The dictionary items function returns the total number of key-value pairs in a given dictionary.

Syntax:

dictionary_name.items()

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.
  • Apply dictionary. items() method to the above-given dictionary and print it.
  • Take a new empty dictionary and store it in another variable.
  • Print the above-initialized new empty dictionary.
  • Apply dictionary. items() method to the above new empty dictionary and print it.
  • 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': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.items() method to the above-given dictionary and print it.
print("The above given dictionary items are :")
print(gvn_dictinry.items())
# Take a new empty dictionary and store it in another variable.
new_dict = {}
# Print the above-initialized new empty dictionary.
print("The above given new dictionary is :")
print(new_dict)
# Apply dictionary.items() method to the above new empty dictionary and print it.
print("The above given New empty dictionary items are :")
print(new_dict.items())

Output:

The above given dictionary is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The above given dictionary items are :
dict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

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.
  • Apply dictionary. items() method to the above-given dictionary and print it.
  • Take a new empty dictionary and store it in another variable.
  • Print the above-initialized new empty dictionary.
  • Apply dictionary. items() method to the above new empty dictionary and print it.
  • 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)
# Apply dictionary.items() method to the above-given dictionary and print it.
print("The above given dictionary items are :")
print(gvn_dictinry.items())
# Take a new empty dictionary and store it in another variable.
new_dict = {}
# Print the above-initialized new empty dictionary.
print("The above given new dictionary is :")
print(new_dict)
# Apply dictionary.items() method to the above new empty dictionary and print it.
print("The above given New empty dictionary items are :")
print(new_dict.items())

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 3
Enter key and value separated by spaces = this 5
Enter key and value separated by spaces = is 7
Enter key and value separated by spaces = btechgeeks 9
The above given dictionary is :
{'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}
The above given dictionary items are :
dict_items([('good', '1'), ('morning', '3'), ('this', '5'), ('is', '7'), ('btechgeeks', '9')])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])