Python program to find the length of dictionary values.

In this python dictionary program, we will find the length of dictionary values.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over the input dictionary.
  3. Add the word as key and its length as value to the empty dictionary using the len() function.
  4. Print the output.
				
					D1 = {1:'sql',2:'tools',3:'python'}
D2 = {}

for val in D1.values():
    D2[val] = len(val)
    
print(D2)
				
			

Output :

				
					{'sql': 3, 'tools': 5, 'python': 6}
				
			

extract a list of values from a given list of dictionaries

get the depth of the dictionary.

Extract a list of values from a given list of dictionaries

In this python dictionary program, we will extract a list of values from a given list of dictionaries

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. First, iterate over dictionaries in the list using a for loop.
  3. Now to iterate over the keys and values of each dictionary use another for loop.
  4. Add the values to the empty list for the specified key.
  5. Print the output.
				
					D1 = [{'t20':50,'odi':70},{'t20':40,'odi':10},{'t20':30,'odi':0},{'t20':45,'odi':65}]
l = []

for ele in D1:
    for key,val in ele.items():
        if key == 'odi':
            l.append(val)
            
print(l)
				
			

Output :

				
					[70, 10, 0, 65]
				
			

update the list values in the said dictionary.

find the length of dictionary values.

Update the list values in the dictionary.

In this python dictionnary program, we will update the list values in the dictionary.

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. Create a function to update the values.
  3. Use for loop to update values for each key of the dictionary.
  4. Add 10 to the values for the 1st key and subtract 15 from the values of the 2nd key.
  5. Return the dictionary.
  6. Now pass the input dictionary to the function to print the output.
				
					def add(dictionary):
    dictionary['virat'] = [x+10 for x in dictionary['virat']]
    dictionary['rohit'] = [x-25 for x in dictionary['rohit']]
    return dictionary

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

print(add(D1))
				
			

Output :

				
					{'virat': [60, 40], 'rohit': [15, -15]}
				
			

clear the list values in the said dictionary.

extract a list of values from a given list of dictionaries

Python program to clear the list values in the dictionary.

In this python dictionary program, we will clear the list values in the dictionary.

Steps to solve the program
  1. Take a dictionary in the given format as input
  2. Use for loop to iterate over values of the dictionary.
  3. Clear the list of values using clear().
  4. Print the output.
				
					D1 = {'virat':[50,30],'rohit':[40,10]} 
for key in D1:
    D1[key].clear()

print(D1)
				
			

Output :

				
					{'virat': [], 'rohit': []}
				
			

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

update the list values in the said dictionary.

Convert string values of a dictionary into integer/float datatypes.

In this python dictionary program, we will convert string values of a dictionary into integer/float datatypes.

Steps to solve the program
  1. Take two dictionaries as input and create two empty dictionaries.
  2. First, use for loop to iterate over keys and values of the 1st dictionaries.
  3. Add the keys and values to the 1st empty dictionary.
  4. While adding them convert the datatype of the value from a string to an integer datatype.
  5. Now repeat the above step for converting the datatype of the values from the 2nd dictionary from string to float.
  6. Add them to the 2nd empty dictionary.
  7. Print the output.
				
					A = { 'a': '30',  'b': '20', 'c': '10' }
D1 = {}
for k,v in A.items():
    D1[k] = int(v)

B =  { 'a': '3.33', 'b': '20.50', 'c': '12.5' } 
D2 = {}
for key,val in B.items():
    D2[key] = float(val)
    
print(D1)
print(D2)
				
			

Output :

				
					{'a': 30, 'b': 20, 'c': 10}
{'a': 3.33, 'b': 20.5, 'c': 12.5}

				
			

remove a specified dictionary from a given list.

clear the list values in the said dictionary.

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.

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.