Remove a specified dictionary from a given list.

In this python dictionary program, we will remove a specified dictionary from a given list.

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 dictionaries in the list using the range() and len() functions.
  3. Use an if statement to add all the dictionaries to the empty list except the 4th dictionary.
  4. Print the output.
				
					A =  [ { 't20':50, 'odi':70 }, { 't20':40, 'odi':10 }, { 't20':30,'odi':0 }, { 't20':45, 'odi':65} ]
B = []
for i in range(len(A)):
    if i != 3:
        B.append(A[i])
        
print(B)
				
			

Output :

				
					[{'t20': 50, 'odi': 70}, {'t20': 40, 'odi': 10}, {'t20': 30, 'odi': 0}]
				
			

split a given dictionary of lists into list of dictionaries.

convert string values of a given dictionary, into integer/float datatypes.

Split a given dictionary of lists into list of dictionaries.

In this python dictionary program, we will split a given dictionary of lists into list of dictionaries.

Steps to solve the program
				
					A={'t20':[50,40,30,45], 'odi':[70,10,0,65]}
def dicts(format_):
    result = map(dict, zip(*[[(key, val) for val in value] for key, value in format_.items()]))
    return list(result)

print(dicts(A))
				
			

Output :

				
					[{'t20': 50, 'odi': 70}, {'t20': 40, 'odi': 10}, {'t20': 30, 'odi': 0}, {'t20': 45, 'odi': 65}]
				
			

create a dictionary grouping a sequence of key-value pairs into a dictionary of lists.

remove a specified dictionary from a given list.

Create a dictionary grouping a sequence of key-value pairs.

In this python dictionary program, we will create a dictionary grouping a sequence of key-value pairs into a dictionary of lists.

Steps to solve the program
  1. Take a dictionary as input.
  2. Create a function and pass a dictionary to it.
  3. Create an empty dictionary inside the function.
  4. Use for loop to iterate over keys and values of the input dictionary.
  5. Add a grouping of a sequence of key-value pairs using setdefault(k, []).append(v), where k is the key and v in the value for that key.
  6. Return the dictionary.
  7. Now pass the input dictionary to the function to print the output.
				
					D1 = [('virat',50), ('rohit',40), ('virat',30), ('rohit',10)]

def grouping_dictionary(l):
    result = {}
    for k, v in l:
         result.setdefault(k, []).append(v)
    return result
print(grouping_dictionary(D1))
				
			

Output :

				
					{'virat': [50, 30], 'rohit': [40, 10]}
				
			

check all values are the same in a dictionary.

split a given dictionary of lists into list of dictionaries.

Check all values are the same in a dictionary.

In this python dictionary program, we will check all values are the same in a dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over values of the dictionary using values().
  3. Use all() to check whether all values are the same in a dictionary.
  4. Print the output.
				
					D1 = {'virat':50, 'rohit':50, 'rahul':50, 'hardik':50}  
result = all(x == 50 for x in D1.values()) 

print(result)
				
			

Output :

				
					True
				
			

convert a matrix into a dictionary.

create a dictionary grouping a sequence of key-value pairs into a dictionary of lists.

Python program to convert a matrix into a dictionary.

In this python dictionary program, we will convert a matrix into a dictionary.

Steps to solve the program
  1. Take a matrix as input and create an empty list.
  2. Use for loop with the range function to iterate over the length of the dictionary using len().
  3. Add 1 to the length and assign this as the key and its corresponding index value as its value.
  4. Print the output.
				
					matrix = [[1,2,3],[4,5,6]]
dict1 = {}
for i in range(len(matrix)):
    dict1[i+1] = matrix[i]
    
dict1
				
			

Output :

				
					{1: [1, 2, 3], 2: [4, 5, 6]}
				
			

convert string to the dictionary.

check all values are the same in a dictionary.

Python program to convert string to the dictionary.

In this python dictionary program, we will convert string to the dictionary.

Steps to solve the program
  1. Take a string as input in the given format.
  2. First split the string on then, split the string on =.
  3. Then convert it to the dictionary using dict().
  4. Print the output.
				
					str1 = "Apr=April; Mar=March"
dictionary = dict(subString.split("=") 
                  for subString in str1.split(";"))

print(dictionary)
				
			

Output :

				
					{'Apr': 'April', ' Mar': 'March'}
				
			

convert a list of Tuples into a dictionary

convert a matrix into a dictionary.

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.