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.

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.