Popular Dictionary Program in Python for Beginner

Most Popular Dictionary Program in Python for Beginner is Written Below,

1. Write a Python script to add a key to a dictionary.

d={}
n=int(input("How many key? "))
i=1
while i<=n:
    n1=input("enter key ")
    n2=input("enter key value")
    d.update({n1:n2})
    i+=1
    print(d)
print("final dictionary is ",d)

Output:

How many key? 5
enter key a
enter key value123
{'a': '123'}
enter key b
enter key value234
{'a': '123', 'b': '234'}
enter key c
enter key value345
{'a': '123', 'b': '234', 'c': '345'}
enter key d
enter key value456
{'a': '123', 'b': '234', 'c': '345', 'd': '456'}
enter key e
enter key value567
{'a': '123', 'b': '234', 'c': '345', 'd': '456', 'e': '567'}
final dictionary is {'a': '123', 'b': '234', 'c': '345', 'd': '456', 'e': '567'}


2. Write a Python program to concatenate following dictionaries
to create a new one.
d1={1:100, 2:200}
d2={3:300, 4:400}
d3={5:500, 6:600}

d={}
d1={1:100, 2:200}
d2={3:300, 4:400}
d3={5:500, 6:600}
d.update(d1)
d.update(d2)
d.update(d3)
print("concatenate dictionary of above dictionaries is ",d)

Output:

concatenate dictionary of above dictionaries is {1: 100, 2: 200, 3: 300, 4: 400, 5: 500, 6: 600}


3. Write a Python program to check if a given key already exists in a dictionary.

d={}
n=int(input("How many key enter? "))
i=1
while i<=n:
    n1=input("enter key ")
    if n1 in d:
        print("key already exist in dic..\nPlease enter another key ")
    else:
        n2=input("enter key value")
        d.update({n1:n2})
        i+=1
print("final dictionary is ",d)

Output:

How many key enter? 5
enter key aa
enter key value999
enter key fff
enter key value7777
enter key hh
enter key value666
enter key a
enter key value1111
enter key fff
key already exist in dic..
Please enter another key

enter key ii
enter key value02020
final dictionary is {'aa': '999', 'fff': '7777', 'hh': '666', 'a': '1111', 'ii': '02020'}


4. Write a Python script to print a dictionary where the keys are
numbers between 1 and 15 (both included) and the values are square of keys.

d={}
i=1
while i in range(1,16):
    d.update({i:i*i})
    i+=1
print("Dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys is \n",d)

Output:

Dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys is
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}


5. Write a Python program to sum all the items in a dictionary.

d={}
n=int(input("How many key? "))
i=1
s=0
while i<=n:
    n1=input("enter key ")
    n2=int(input("enter key value"))
    d.update({n1:n2})
    i+=1
print("Your dictionary is ",d)
for i in d.values():
    s=s+i
print("Sum all the items in a dictionary is ",s)

Output:

How many key? 4
enter key a
enter key value10
enter key b
enter key value88
enter key c
enter key value55
enter key f
enter key value90
Your dictionary is {'a': 10, 'b': 88, 'c': 55, 'f': 90}
Sum all the items in a dictionary is 243


6. Write a Python program to remove a key from a dictionary.
D = {'a':1,'b':2,'c':3,'d':4}

D = {'a':1,'b':2,'c':3,'d':4}
i=0
while i==0:
    n = input("Enter Key ")
    if n in D:
        D.pop(n)
        i+=1
    else:
        print("key is not found")

    print("Dictionary is ",D)

Output:

Enter Key 4
key is not found
Dictionary is {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Enter Key e
key is not found
Dictionary is {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Enter Key c
Dictionary is {'a': 1, 'b': 2, 'd': 4}


7. Write a Python program to sort a dictionary by key.

d={}
n=int(input("How many key? "))
i=1
while i<=n:
    n1=input("enter key ")
    n2=input("enter key value")
    d.update({n1:n2})
    i+=1
print("Dictionary is ",d)
s=sorted(d)
d1 = {}
for j in s:
       d1.update({j:d.get(j)})
print("Sorted dictionary by key is ",d1)

Output:

How many key? 4
enter key 9
enter key value111
enter key 5
enter key value999
enter key 1
enter key value8888
enter key 3
enter key value444444
Dictionary is {'9': '111', '5': '999', '1': '8888', '3': '444444'}
Sorted dictionary by key is {'1': '8888', '3': '444444', '5': '999', '9': '111'}


8. Write a Python program to remove duplicates from Dictionary.

d={}
n=int(input("How many key enter? "))
i=1
while i<=n:
    n1=input("enter key ")
    n2=input("enter key value")
    d.update({n1:n2})
    i+=1
print("Dictionary is ",d)

d1={}
for j in d:
       if d.get(j) not in d1.values():
            d1.update({j:d.get(j)})
print("Dictionary without duplicate value is ",d1)

Output:

How many key enter? 4
enter key yy
enter key value909
enter key ww
enter key value3030
enter key yy
enter key value6060
enter key ss
enter key value909
Dictionary is {'yy': '6060', 'ww': '3030', 'ss': '909'}
Dictionary without duplicate value is {'yy': '6060', 'ww': '3030', 'ss': '909'}


9.Write a program to determine frequency of number in a list of numbers.

l=int(input("How many numbers are you enter in your list? "))
i=1
lst=[]
while i<=l:
    ls=int(input("enter a number "))
    lst.append(ls)
    i+=1
print("Your list is ",lst)
dic={}
for j in lst:
    c=lst.count(j)
    dic.update({j:c})
print("Dictionary of frequency of number in a list of numbers",dic)

Output:

How many numbers are you enter in your list? 5
enter a number 10
enter a number 20
enter a number 30
enter a number 10
enter a number 30
Your list is [10, 20, 30, 10, 30]
Dictionary of frequency of number in a list of numbers {10: 2, 20: 1, 30: 2}


I Think You Understand above Topic "Popular Dictionary Program in Python for Beginner."
For any query or suggestions feel free to fill comment section and send it.


More Topic of Python:
Python Program of Basic Question’s Part-1
Python Program of Basic Question’s Part-2
Top Python Program of String for beginner
Highly Recommended Python Program of List for beginner
Mind Blowing While Loop program for beginner

You may also like...

Leave a Reply

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