Convert a list of Tuples into a dictionary

In this python dictionary program, we will convert a list of Tuples into a dictionary

Steps to solve the program
  1. Take a list of tuples as input and create an empty list.
  2. Use for loop to iterate over a list of tuples.
  3. From tuples add 1st element as the key and 2nd element as its value to the dictionary.
  4. Print the output.
				
					list1 = [("mike",1),("sarah",20),("jim", 16)]
dict1 = {}
for val in list1:
    dict1[val[0]] = val[1]
    
print(dict1)
				
			

Output :

				
					{'mike': 1, 'sarah': 20, 'jim': 16}
				
			

convert a key-values list to a flat dictionary.

convert string to the dictionary.

Convert a key-values list to a flat dictionary

In this python dictionary program, we will convert a key-values list to a flat dictionary.

Steps to solve the program
  1. Take a list having a list of values as input.
  2. Convert a key-values list to a flat dictionary using zip() and dict().

  3. Zip() will convert the list of values into a key-value pair.
  4. Print the output.
				
					dict1 = {'name':['Apr','May','June'],'month':[4, 5, 6]}

res = dict(zip(dict1['name'],dict1['month']))

print(res)
				
			

Output :

				
					{'Apr': 4, 'May': 5, 'June': 6}
				
			

filter a dictionary based on values.

convert a list of Tuples into a dictionary

Python program to filter a dictionary based on values.

In this python dictionary program, we will filter a dictionary based on values.

Steps to solve the program
  1. Take a dictionary as input and create en empty dictionary.
  2. Use for loop to iterate over keys and values of the input dictionary.
  3. Add the key-value pairs in the empty dictionary for which values are greater than 40.
  4. Use a conditional statement for this purpose.
  5. Print the output.
				
					dict1 = {'alex':50,'john':45,'Robert' :30}
dict2 = {}

for key,val in dict1.items():
    if val > 40:
        dict2[key] = val
        
print(dict2)
				
			

Output :

				
					{'alex': 50, 'john': 45}
				
			

drop empty Items from a given dictionary.

convert a key-values list to a flat dictionary.

Python program to drop empty Items from a given dictionary.

In this python dictionary program, we will drop empty Items from a given dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Using a for loop and an if-else statement add the key-value pairs from the input dictionary whose value is not empty to the empty dictionary.
  3. Print the output.
				
					dict1 = {"m1":40,"m2":50,"m3":None}
dict2 = {}
for key,val in dict1.items():
    if val != None:
        dict2[key] = val
        
dict2
				
			

Output :

				
					{'m1': 40, 'm2': 50}
				
			

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. 

filter a dictionary based on values.

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.