Write a program to find the number of key-value pairs in a dictionary

SOLUTION.

# Creating an empty dictionary
my_dict = {}

# Taking dynamic input from the user
n = int(input("Enter the number of key-value pairs: "))

# Adding key-value pairs dynamically
for i in range(n):
    key = input(f"Enter key {i+1}: ")
    value = input(f"Enter value for '{key}': ")
    my_dict[key] = value

# Displaying the dictionary
print("\nDictionary:", my_dict)

# Finding the number of key-value pairs
num_pairs = len(my_dict)
print("Number of key-value pairs in the dictionary:", num_pairs)
    

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.