Python program to create a dictionary

In this python dictionary program, we will create a dictionary of keys a, b, and c where each key has as value a list from 1-5, 6-10, and 11-15 respectively. 

Steps to solve the program
  1. Create three empty lists and an empty dictionary.
  2. In the first empty list add numbers between 1-5 using a for loop.
  3. Repeat this process for the second and third lists to add respective numbers.
  4. Add the three lists with respective keys to the empty dictionary.
  5. Print the output.
				
					l1= []
for i in range(1,6):
    l1.append(i)
    
l2 = []
for i in range(6,11):
    l2.append(i)
    
l3 = []
for i in range(11,16):
    l3.append(i)
    
dict1 = {}
dict1["a"] = l1
dict1["b"] = l2
dict1["c"] = l3

print(dict1)
				
			

Output :

				
					{'a': [1, 2, 3, 4, 5], 'b': [6, 7, 8, 9, 10], 'c': [11, 12, 13, 14, 15]}
				
			

match key values in two dictionaries.

drop empty Items from a given dictionary.

Python program to match key values in two dictionaries.

In this python dictionary program, we will match key values in two dictionaries.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Use for loop with an if-else statement to check whether keys from the first dictionary exist in the second dictionary or not.
  3. Print the respective output.
				
					dict1 = {'k1':'p','k2':'q','k3':'r'}
dict2 = {'k1':'p','k2':'s'}

for key in dict1:
    if key in dict2:
        print(f"{key} is present in both dictionaries")
    else:
        print(f"{key} is present not in both dictionaries")
				
			

Output :

				
					k1 is present in both dictionaries
k2 is present in both dictionaries
k3 is present not in both dictionaries
				
			

replace dictionary values with their average.

create a dictionary of keys a, b, and c where each key has as value a list from 1-5, 6-10, and 11-15 respectively.

Python program to replace dictionary values with their average.

In this python dictionary program, we will replace dictionary values with their average.

Steps to solve the program
  1. Take a dictionary in a list as input.
  2. Use for loop to iterate over the dictionary.
  3. Create two variables and assign their values equal to the values of the respective keys using pop().
  4. Create a new key and assign its value equal to the average of the above two variables.
  5. Add the above key to the dictionary.
  6. Print the output.
				
					dict1 = [{'name':'ketan','subject':'maths','p1':80,'p2':70}]
for d in dict1:
    n1 = d.pop('p1')
    n2 = d.pop('p2')
    d['p1+p2'] = (n1 + n2)/2
    
print(dict1)
				
			

Output :

				
					[{'name': 'ketan', 'subject': 'maths', 'p1+p2': 75.0}]
				
			

sort items in a dictionary in descending order.

match key values in two dictionaries.

Sort items in a dictionary in descending order.

In this python dictionary program, we will sort items in a dictionary in descending order.

Steps to solve the program
  1. Take a dictionary as input.
  2. Sort the dictionary by values in descending order using sorted().
  3. Use values as the key while sorting using the lamdba function.
  4. Pass values of the dictionary to the lambda function.
  5. To sort the dictionary in descending order assign reverse equal to True.
  6. Print the output.
				
					dict1 = {'Math':70,'Physics':90,'Chemistry':67}

a = sorted(dict1.items(), key = lambda val:val[1],reverse = True)

print(a)
				
			

Output :

				
					[('Physics', 90), ('Math', 70), ('Chemistry', 67)]

				
			

count a number of items in a dictionary value that is in a list.

replace dictionary values with their average.

Count a number of items in a dictionary value that is in a list.

In this python dictionary program, we will count a number of items in a dictionary value that is in a list.

Steps to solve the program
  1. Take a dictionary as input and create a variable and assign its value equal to 0.
  2. Use for loop to iterate over values of the dictionary.
  3. Use a nested for loop to iterate over the value list.
  4. Add 1 to the variable for each item in the value list.
  5. Print the output.
				
					dict1 = {'virat':['match1','match2','match3'],'rohit':['match1','match2']}
count = 0

for val in dict1.values():
    if type(val)==list:
        for ele in val:
            count += 1

print("Items in the list of values: ",count)
				
			

Output :

				
					Items in the list of values:  5
				
			

convert a list of dictionary to a list of lists.

sort items in a dictionary in descending order.

Convert a list of dictionaries to a list of lists.

In this python dictionary program, we will convert a list of dictionaries to a list of lists.

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. Use for loop to iterate over the dictionary in the list.
  3. Use a nested for loop to iterate over keys of the dictionary and create an empty list inside the loop.
  4. Add the keys to that list and add the list to the first empty list.
  5. Repeat this process for the values of the dictionary.
  6. Print the output.
				
					dict1 = [{'sqa':123,'tools':456}]
l = []
for i in dict1:
    for k in i.keys():
        a = []
        a.append(k)
        l.append(a)
    for v in i.values():
        b = []
        b.append(v)
        l.append(b)

print(l)
				
			

Output :

				
					[['sqa'], ['tools'], [123], [456]]
				
			

convert a key value list dictionary into a list of list.

count a number of items in a dictionary value that is in a list.

Convert a key value list dictionary into a list of lists.

In this python dictionary program, we will convert a key value list dictionary into a list of lists.

Steps to solve the program
  1. Take a dictionary as input and create an empty list.
  2. Use for loop to iterate over keys and values of the input dictionary.
  3. Create an empty list inside the for loop.
  4. First, add the key to the empty list using append().
  5. Now using a nested for loop iterate over a list of values and add them to the empty list using append().
  6. Now add this list to the first empty list.
  7. Repeat this process for each key-value pair.
  8. Print the output.
				
					dict1 = {'sqa':[1,4,6],'tools':[3,6,9]}
list1 = []

for key,val in dict1.items():
    l = []
    l.append(key)
    for ele in val:
        l.append(ele)
    list1.append(list(l))
    
print(list1)
				
			

Output :

				
					[['sqa', 1, 4, 6], ['tools', 3, 6, 9]]
				
			

print a dictionary line by line.

convert a list of dictionary to a list of lists.

Python program to print a dictionary line by line.

In this python dictionary program, we will print a dictionary line by line.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over keys and values of the dictionary using items().
  3. First, print the key.
  4. Using nested for loop iterate over keys and values in the value of the respective key.
  5. Print the key-value pair.
				
					dict1 = {'virat':{'sport':'cricket','team':'india'},
         'messi':{'sport':'football','team':'argentina'}}
for key, val in dict1.items():
    print(key)
    for k,v in val.items():
        print(k,":",v)
				
			

Output :

				
					virat
sport : cricket
team : india
messi
sport : football
team : argentina
				
			

get a product with the highest price from a dictionary.

convert a key value list dictionary into a list of lists.

Get a product with the highest price from a dictionary.

In this python dictionary program, we will get a product with the highest price from a dictionary.

Steps to solve the program
  1. Take a dictionary of products as input.
  2. Create two variables to store the product name and the product price.
  3. Assign their values equal to 0.
  4. Use for loop to iterate over keys and values of the input dictionary using items().
  5. If the value of the product is greater than the variable that we have created to store the product price and name, then assign the value of the variable to the corresponding product name and price.
  6. Repeat this process for all the products
  7. Print the output.
				
					dict1 = {'price1':450,'price2':600,'price3':255,'price4':400}
p_name = 0
p_price = 0

for key,val in dict1.items():
    if val > p_price:
        p_price = val
        p_name = key

print("Product name: ",p_name)
print("Product price: ",p_price)
				
			

Output :

				
					Product name:  price2
Product price:  600
				
			

sort a list of values in a dictionary.

print a dictionary line by line.

Sort a list of values in a dictionary.

In this python dictionary program, we will sort a list of values in a dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over a list of values of the given dictionary using values().
  3. During iteration sort the list of values using sort().
  4. Print the output.
				
					dict1 = {'a1':[1,5,3],'a2':[10,6,20]}

for val in dict1.values():
    val.sort()
    
print(dict1)
				
			

Output :

				
					{'a1': [1, 3, 5], 'a2': [6, 10, 20]}
				
			

convert a list into a nested dictionary of keys

get a product with the highest price from a dictionary.