Share @ LinkedIn Facebook  python, basics
Python Programming Examples on Dictionary

Python Programming Examples on Dictionary

Working on simple programs can help you to gain an immense understanding of the logic that is used in the code.

What You Would Learn From This Tutorial

  • Program to Add a Key-Value Pair to the Dictionary
  • Program to Concatenate Two Dictionaries Into One
  • Program to Check if a Given Value Exists in a Dictionary or Not
  • Program to Sum All the Items in a Dictionary
  • Program to Multiply All the Items in a Dictionary
  • Program to Remove the Given Value from a Dictionary
  • Program to Count the Frequency of Words Appearing in a String Using a Dictionary

Program to Add a Key-Value Pair to the Dictionary

In [1]:
key=int(input("Enter the key (int) to be added:"))
value=int(input("Enter the value for the key to be added:"))
d={}
d.update({key:value})
print("Updated dictionary is:")
print(d)
Enter the key (int) to be added:23
Enter the value for the key to be added:2
Updated dictionary is:
{23: 2}

Understanding The Program

  1. User must enter a key-value pair and store it in separate variables.
  2. A dictionary is declared and initialized to an empty dictionary.
  3. The update() function is used to add the key-value pair to the dictionary.
  4. The final dictionary is printed.

Program to Concatenate Two Dictionaries Into One

In [1]:
d1={'A':1,'B':2}
d2={'C':3}
d1.update(d2)
print("Concatenated dictionary is:")
print(d1)
Concatenated dictionary is:
{'A': 1, 'B': 2, 'C': 3}

Understanding The Program

  1. User must enter declare and initialize two dictionaries with a few key-value pairs and store it in separate variables.
  2. The update() function is used to add the key-value pair from the second to the first dictionary.
  3. The final updated dictionary is printed.

Program to Check if a Given Value Exists in a Dictionary or Not

In [3]:
d={'A':1,'B':2,'C':3}
key=input("Enter key to check:")
if key in d.keys():
      print("Key is present and value of the key is:")
      print(d[key])
else:
      print("Key isn't present!")
Enter key to check:3
Key isn't present!

Understanding The Program

  1. User must enter the key to be checked and store it in a variable.
  2. An if statement and the in operator is used check if the key is present in the list containing the keys of the dictionary.
  3. If it is present, the value of the key is printed.
  4. If it isn’t present, “Key isn’t present!” is printed.
  5. Exit.

Program to Sum All the Items in a Dictionary

In [5]:
d={'A':100,'B':540,'C':239}
print("Total sum of values in the dictionary:")
print(sum(d.values()))
Total sum of values in the dictionary:
879

Understanding The Program

  1. The sum() function is used to find the sum of all the values in the dictionary.
  2. The total sum of all the values in the dictionary is printed.
  3. Exit.

Program to Multiply All the Items in a Dictionary

In [6]:
d={'A':10,'B':10,'C':239}
tot=1
for i in d:
    tot=tot*d[i]
print(tot)
23900

Understanding The Program

  1. A dictionary is declared and initialized to have some key-value pairs.
  2. The total variable is initialized to 1.
  3. The for loop is used to traverse through the values of the dictionary.
  4. Then all the values in the dictionary are multiplied against each other.
  5. The total multiplied value is printed.

Program to Remove the Given Value from a Dictionary

In [1]:
d = {'a':1,'b':2,'c':3,'d':4}
print("Initial dictionary")
print(d)
key=input("Enter the key to delete(a-d):")
if key in d:
    del d[key]
else:
    print("Key not found!")
    exit(0)
print("Updated dictionary")
print(d)
Initial dictionary
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
Enter the key to delete(a-d):b
Updated dictionary
{'a': 1, 'c': 3, 'd': 4}

Understanding The Program

  1. User must enter the key to be checked and store it in a variable.
  2. An if statement and the in operator is used check if the key is present in the dictionary.
  3. If it is present, the key-value pair is deleted.
  4. If it isn’t present, “Key not found!” is printed and the program is exited.

Program to Count the Frequency of Words Appearing in a String Using a Dictionary

In [3]:
test_string=input("Enter string:")
l=[]
l=test_string.split()
wordfreq=[l.count(p) for p in l]
print(dict(zip(l,wordfreq)))
Enter string:Hello Dolly
{'Hello': 1, 'Dolly': 1}

Understanding The Program

  1. User must enter a string and store it in a variable.
  2. A list variable is declared and initialized to an empty list.
  3. The string is split into words using a space as the reference and stored in the list.
  4. The frequency of each word in the list is counted using list comprehension and the count() function.
  5. The final dictionary is created using the zip() function and is printed.

Dolly Solanki  Dolly Solanki