Convert given a dictionary to a list of tuples.

In this python dictionary program, we will convert given a dictionary to a list of tuples.

Steps to solve the program
  1. Take a dictionary as input.
  2. Convert the dictionary to a list of tuples using list() and items().
  3. Print the output.
				
					D1 = {'a':19,'b':20,'c':21,'d':20}
l = list(D1.items())
    
print(l)
				
			

Output :

				
					[('a', 19), ('b', 20), ('c', 21), ('d', 20)]
				
			

find all keys in the provided dictionary that have the given value.

create a flat list of all the keys in a flat dictionary.

Find all keys in the provided dictionary that have the given value.

In this python dictionary program, we will find all keys in the provided dictionary that have the given value.

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 dictionary.
  3. If a value is equal to 0 then add its key to the list using append().
  4. Print the output.
				
					D1 = {'a':19,'b':20,'c':21,'d':20}
l = []

for k,v in D1.items():
    if v == 20:
        l.append(k)
        
print(l)
				
			

Output :

				
					['b', 'd']
				
			

convert a list of dictionaries into a list of values corresponding to the specified key.

convert given a dictionary to a list of tuples.

Convert a list of dictionaries into a list of values.

In this python dictionary program, we will convert a list of dictionaries into a list of values corresponding to the specified key.

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 dictionaries in the list.
  3. Get the value for the specified key in the dictionary using the get() function and add that value to the empty list.
  4. Print the output.
				
					l1 =  [{'name':'josh','age':30},{'name':'david','age':25},{'name':'virat','age':32}]
result = []

for x in l1:
    result.append(x.get('age'))

print(result)
				
			

Output :

				
					[30, 25, 32]

				
			

show keys associated with values in dictionary

find all keys in the provided dictionary that have the given value.

Show keys associated with values in dictionary.

In this python dictionary program, we will show keys associated with values in dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over key-value pairs in the input dictionary.
  3. Use a nested for loop to iterate over elements in the values list.
  4. Add the element as the key and the key for that value list as its value to the empty dictionary.
  5. Print the output.
				
					D1 = {'xyz':[20,40],'abc':[10,30]}
D2 = {}

for k,v in D1.items():
    for ele in v:
        D2[ele] = [k]
        
print(D2)
				
			

Output :

				
					{20: ['xyz'], 40: ['xyz'], 10: ['abc'], 30: ['abc']}

				
			

Extract Unique values from dictionary values.

convert a list of dictionaries into a list of values corresponding to the specified key.

Python program to extract unique values from dictionary values.

In this python dictionary program, we will extract unique values from dictionary values.

Steps to solve the program
  1. Take a dictionary in the given format as input and create an empty dictionary.
  2. Use for loop to iterate over the values in the dictionaries.
  3. Use a nested for loop to iterate the value list.
  4. If the element in the value list is not in the empty list then add that element to it using the append() function.
  5. Print the output.
				
					D1= {'sqa':[1,2,5,6],'tools':[3,8,9],'is':[2,5,0],'best':[3,6,8]}
value = []

for val in D1.values():
    for ele in val:
        if ele not in value:
            value.append(ele)
            
print(value)
				
			
				
					[1, 2, 5, 6, 3, 8, 9, 0]
				
			

show a dictionary with a maximum count of pairs.

show keys associated with values in dictionary

Show a dictionary with a maximum count of pairs.

In this python dictionary program, we will show a dictionary with a maximum count of pairs.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Count the key-value pairs in both dictionaries using the len() function.
  3. Compare them by using ‘ > ‘ or ‘ ‘.
  4. Print the respective output.
				
					A = {'name':1,'age':2}
B = {'name':1,'age':2,'course':3,'institute':4}

count_a = len(A)
count_b = len(B)

if count_a > count_b:
    print("1st dicyionary has maximum pairs",count_a)
else:
    print("2nd dicyionary has maximum pairs",count_b)
				
			

Output :

				
					2nd dicyionary has maximum pairs 4
				
			

retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Extract Unique values from dictionary values.

Retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

In this python dictionary program, we will retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Steps to solve the program
  1. From functools import reduce.
  2. From operator import getitem.
  3. Take a nested dictionary as input.
  4. Create a function to retrieve the value of the nested key indicated by the given selector list from a dictionary.
  5. Give 2 parameters to the function as input one is the dictionary and the second is the selectors.
  6. Using reduce and getitem retrieve the value of the nested key indicated by the given selector.
  7. Print the output.
				
					from functools import reduce 
from operator import getitem
D1 = {'p1':{'name':{'first':'lionel','last':'messi'},'team':['psg','argentina']}}

def retrive(d, selectors):
    return reduce(getitem, selectors, d)

print(retrive(D1, ['p1','name','last']))
print(retrive(D1, ['p1','team', 1]))
				
			

Output :

				
					messi
argentina
				
			

group the elements of a given list based on the given function.

show a dictionary with a maximum count of pairs.

Group the elements of a list based on the function.

In this python dictionary program, we will group the elements of a list based on the function.

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Use for loop to iterate over the words in the dictionary.
  3. Add the word length as the key using the len() function and the word as its value to the empty dictionary.
  4. Print the output.
				
					l = ['abc','defg','hijkl']
D1 = {}

for val in l:
    D1[len(val)] = val
    
print(D1)
				
			

Output :

				
					{3: 'abc', 4: 'defg', 5: 'hijkl'}
				
			

get the total length of a dictionary with string values.

retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Python program to get the total length of a dictionary.

In this python dictionary program, we will get the total length of a dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use the len() function to calculate the length of the dictionary.
  3. The len() function will return the key-value pairs in the dictionary.
  4. To get the total number of items in the dictionary multiply the len() function by 2.
  5. Print the output.
				
					D1 = {'virat':50,'Rohit':40,'Rahul':25}

print(len(D1)*2)
				
			

Output :

				
					6
				
			

create key-value list pairings in a given dictionary.

group the elements of a given list based on the given function.

Create key-value list pairings in a given dictionary

In this python dictionary program, we will create key-value list pairings in a given dictionary

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. From itertools import product.
  3. Use for loop and pass the values to the product() function.
  4. Using the zip() function combine the key and its respective value as a single key-value pair.
  5. Convert these pairs into a dictionary using dict() and store the result in a variable.
  6. Print the dictionary inside a list.
				
					from itertools import product
D1 = {1:['Virat Kohli'],2:['Rohit Sharma'],3:['Hardik Pandya']}

for sub in product(*D1.values()):
    result = [dict(zip(D1, sub))]
    
print(result)
				
			

Output :

				
					[{1: 'Virat Kohli', 2: 'Rohit Sharma', 3: 'Hardik Pandya'}]
				
			

count the frequency in a given dictionary.

get the total length of a dictionary with string values.