Convert a list into a nested dictionary of keys.

In this python dictionary program, we will convert a list into a nested dictionary of keys.

Steps to solve the program
  1. Take a list of characters as input and create two empty dictionaries.
  2. Use for loop to iterate over characters in the list.
  3. Add the character as the key and an empty dictionary as its value.
  4. Now add the above key to the empty dictionary.
  5. Print the output.
				
					list1 = ['a','b','c','d']
new_dict = current = {}

for char in list1:
    current[char] = {}
    current = current[char]
print(new_dict)
				
			

Output :

				
					{'a': {'b': {'c': {'d': {}}}}}
				
			

find mean of values of keys in a dictionary.

sort a list of values in a dictionary.

Find the mean of values of keys in a dictionary.

In this python dictionary program, we will find the mean of values of keys in a dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Create two variables named total and count to find the mean of the values.
  3. Use for loop to iterate over values in the dictionary.
  4. Add the value to the total variable and 1 to the count variable.
  5. Find the mean by using the logic.
				
					dict1 = {'m1':25,'m2':20,'m3':15}
total = 0
count = 0

for val in dict1.values():
    total += val
    count += 1
    
print("Mean: ",(total/count))
				
			

Output :

				
					Mean:  20.0
				
			

count frequencies in a list using a dictionary.

convert a list into a nested dictionary of keys.

Count frequencies in a list using a dictionary.

In this python dictionary program, we will count frequencies in a list using a dictionary.

Steps to solve the program
  1. Take a list as input and create an empty dictionary.
  2. Use for loop to iterate over elements in the list.
  3. Add the element as key and its occurrences in the list as its value.
  4. Use count() for this purpose.
  5. Print the output.
				
					list1 = [2,5,8,1,2,6,8,5,2]
dict1 = {}

for val in list1:
    dict1[val] = list1.count(val)
    
print(dict1)
				
			

Output :

				
					{2: 3, 5: 2, 8: 2, 1: 1, 6: 1}
				
			

print the given dictionary in the form of tables.

find mean of values of keys in a dictionary.

Print the given dictionary in the form of tables.

In this python dictionary program, we will print the given dictionary in the form of tables.

Steps to solve the program
  1. Take a dictionary as input.
  2. Import pandas library.
  3. Print the given dictionary in the form of tables using Dataframe() from pandas.
				
					import pandas as pd
dict1 = {'names':['virat','messi','kobe'],'sport':['cricket','football','basketball']}

table = pd.DataFrame(dict1)

print(table)
				
			

Output :

				
					   names       sport
0  virat     cricket
1  messi    football
2   kobe  basketball
				
			

create a dictionary from a string.

count frequencies in a list using a dictionary.

Python program to create a dictionary from a string.

In this python dictionary program, we will create a dictionary from a string.

Steps to solve the program
  1. Take a string as input and create an empty dictionary.
  2. Use for loop to iterate over characters in the dictionary.
  3. Add the character as the key and its occurrences in the dictionary as its value to the empty dictionary.
  4. Use count() to find the occurrences.
  5. Print the output.
				
					str1 = "sqatools"
dict1 = {}

for char in str1:
    dict1[char] = str1.count(char)
    
print(dict1)
				
			

Output :

				
					{'s': 2, 'q': 1, 'a': 1, 't': 1, 'o': 2, 'l': 1}
				
			

display different combinations of letters from dictionary values.

print the given dictionary in the form of tables.

Display different combinations of letters from dictionary values.

In this python dictionary program, we will display different combinations of letters from dictionary values.

Steps to solve the program
				
					import itertools      
d ={'x':['e','f'],'y':['a','b']}

for combi in itertools.product(*[d[char] for char in sorted(d.keys())]):
    print(''.join(combi))
				
			

Output :

				
					ea
eb
fa
fb
				
			

print all the unique values in a dictionary.

create a dictionary from a string.

Python program to print all the unique values in a dictionary.

In this python dictionary program, we will print all the unique values in a dictionary.

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. Use a for loop to iterate over each dictionary in the list.
  3. Use a nested for loop to iterate over each dictionary value.
  4. If the value is not in the list that we have created, then add that value to the list using append().
  5. Print the output.
				
					list1 = [{'name1':'robert'},{'name2':'john'},{'name3':'jim'},
         {'name4':'robert'}]
list2 = []
for val in list1:
    for ele in val.values():
        if ele not in list2:
            list2.append(ele)
            
print(list2)
				
			

Output :

				
					['robert', 'john', 'jim']
				
			

add two dictionaries if the keys are the same then add their value.

display different combinations of letters from dictionary values.

Add two dictionaries if the keys are the same then add their value.

In this python dictionary program, we will add two dictionaries if the keys are the same then add their value.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Using a nested for loop and an if-else statement add values of the common keys between the two dictionaries.
  3. Print the output.
				
					Dict1 = {'x':10,'y':20,'c':50,'f':44 }
Dict2 = {'x':60,'c':25,'y':56}

for key in Dict1:
    for k in Dict2:
        if key == k:
            a = Dict1[key]+Dict2[k]
            Dict2[key] = a
        else:
            pass
            
Dict2
				
			

Output :

				
					{'x': 70, 'c': 75, 'y': 76}
				
			

check whether a dictionary is empty or not.

print all the unique values in a dictionary.

Check whether a dictionary is empty or not.

In this python dictionary program, we will check whether a dictionary is empty or not.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use an if-else statement with the len() function to check whether a dictionary is empty or not.
  3. Use logic to print the output.
				
					dict1 = {}
    
if len(dict1) > 0:
    print("Dictionary is not empty")
else:
    print("Dictionary is empty")
				
			

Output :

				
					Dictionary is empty
				
			

remove duplicate values from dictionary values.

add two dictionaries if the keys are the same then add their value.

Program to remove duplicate values from dictionary values.

In this python dictionary program, we will remove duplicate values from 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 keys and values of the dictionary.
  3. Use a nested for loop to iterate over the value list.
  4. Using an if statement check for duplicate values. 
  5. If any duplicate value exists then remove it from the value list using remove().
  6. Add the key and the value list to the empty dictionary.
  7. Print the output.
				
					dict1 = {'marks1':[23,28,23,69],'marks2':[ 25, 14, 25]}

for key,val in dict1.items():
    for ele in val:
        if ele in val:
            val.remove(ele)

print(dict1)
				
			

Output :

				
					{'marks1': [28, 69], 'marks2': [14, 56]}
				
			

remove a word from the string if it is a key in a dictionary.

check whether a dictionary is empty or not.