Program to swap the values of the keys in the dictionary.

In this python dictionary program, we will swap the values of the keys in the dictionary.

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 given dictionary using items().
  3. Swap the keys and values while adding to the other dictionary
  4. Print the output.
				
					D1 = {'name':'yash','city':'pune'}
D2 ={}

for key,value in D1.items():
    D2[value] = key
    
print(D2)
				
			

Output :

				
					{'yash': 'name', 'pune': 'city'}
				
			

concatenate two dictionaries

get the sum of all the items in a dictionary.

Python program to concatenate two dictionaries to from a single dictionary

In this python dictionary program, we will concatenate two dictionaries to form a single dictionary.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Concatenate two dictionaries using update().
  3. Print the output.
				
					dict1 = {'name':'yash','city':'pune'}
dict2 =  {'course':'python','institute':'sqatools'}

dict1.update(dict2)

print(dict1)
				
			

Output :

				
					{'name': 'yash', 'city': 'pune', 'course': 'python', 'institute': 'sqatools'}
				
			

add a key in a dictionary.

swap the values of the keys in the dictionary.

Python program to add a key in a dictionary.

In this python dictionary program, we will add a key in a dictionary to make a new dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Add a key in the given dictionary using update().
  3. Print the output.
				
					dict1 = {1:'a',2:'b'}

dict1.update({3:'c'})

print(dict1)
				
			

Output :

				
					{1: 'a', 2: 'b', 3: 'c'}
				
			

dictionary in python using values.

concatenate two dictionaries

Python program to sort a dictionary using values.

In this python dictionary program, we will sort a dictionary using values.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over keys and values of the dictionary by using dictionary.items() and also use sorted() method.
  3. Use the lambda function to sort the dictionary by values
  4. Use value as the key to sort the dictionary in the sorted() method.
  5. Print the output.
				
					dict1 = {'d':14,'b':52,'a':13,'c': 1}

sorted_ = {key: value for key, value in 
             sorted(dict1.items(), key=lambda item: item[1])}
print(sorted_)
				
			

Output :

				
					{'c': 1, 'a': 13, 'd': 14, 'b': 52}
				
			

sort a dictionary in python using keys.

add a key in a dictionary.

Python program to sort a dictionary using keys.

In this python dictionary program, we will sort a dictionary using keys.

Steps to solve the program
  1. Take a dictionary as input.
  2. Sort the given dictionary by keys using sorted() method.
  3. Print the output.
				
					dict1 = {'d':21,'b':53,'a':13,'c':41}

for key in sorted(dict1):
    print("%s: %s" % (key,dict1[key]))
				
			

Output :

				
					a: 13
b: 53
c: 41
d: 21
				
			

create a dictionary from the string.

sort a dictionary in python using values.

Python program to create a dictionary from the string.

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

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

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

print(dict1)
				
			

Output :

				
					{'S': 1, 'Q': 1, 'A': 1, 'T': 1, 'o': 2, 'l': 1, 's': 2}
				
			

remove duplicate values from Dictionary.

sort a dictionary in python using keys.

Python program to remove duplicate values from Dictionary.

In this python dictionary program, we will remove duplicate values from Dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary and list.
  2. Use for loop to iterate over keys and values of the dictionary using dictionary.items().
  3. If the value is not in the list then add that value and its key to the empty dictionary.
  4. Print the output.
				
					dict1 = {'a':12,'b':2,'c':12,'d':5,'e':35,'f':5}
dict2 = {}

for key,val in dict1.items():
    if val not in dict2.values():
        dict2[key] = val

print(dict2)
				
			

Output :

				
					{'a': 12, 'b': 2, 'd': 5, 'e': 35}
				
			

clear all items from the dictionary.

create a dictionary from the string.

Python program to clear all items from the dictionary.

In this python dictionary program, we will clear all items from the dictionary using an in-built function.

Steps to solve the program
  1. Take a dictionary as input.
  2. Clear all items from the given dictionary using clear().
  3. Print the output.
				
					dict1 = {'Name':'Harry','Rollno':345,'Address':'Jordan'}

dict1.clear()
print(dict1)
				
			

Output :

				
					{}
				
			

store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.

remove duplicate values from Dictionary.

Store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.

In this python dictionary program, we will store squares of even and cubes of odd numbers in a dictionary using the dictionary comprehension method.

Steps to solve the program
  1. Take a list as input and create an empty dictionary.
  2. Use for loop to iterate over list elements.
  3. If an element is an even number then store its square and if it is an odd number then store its cube in the dictionary.
  4. Use an if-else statement to check for odd and even numbers.
  5. Print the output.
				
					list1 = [4, 5, 6, 2, 1, 7, 11]
dict1 = {}

# iterate through the list of values
for val in list1:
    # val is divisible by 2, then value is even number
    if val % 2 == 0:
        # dict store value as square
        dict1[val] = val**2
    else:
        # dict store value as cube
        dict1[val] = val**3
    
print("result dictionary:", dict1)
				
			

 Output :

				
					result dictionary : {4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
				
			


Solution 2: solution with dictionary comprehension.
				
					input_list = [4, 5, 6, 2, 1, 7, 11]
dict_result={x:x**2 if x%2==0 else x**3 for x in input_list}
print("output dictionary :", dict_result)
				
			
 
Output:
				
					output dictionary : {4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
				
			

create a dictionary from two lists.

clear all items from the dictionary.

Python Program to create a dictionary from two lists.

In this python dictionary program, we will create a dictionary from two lists. Elements from the first list as keys and elements from the second as its values.

Steps to solve the program
  1. Take two lists as inputs and create an empty dictionary.
  2. Combined both lists by using the zip() function.
  3. Use for loop to iterate over the combined list.
  4. During iteration add elements from the first list as keys and elements from the second list as its values.
  5. Print the output.
				
					list1 = ['a','b','c','d','e']
list2 = [12,23,24,25,15,16]
dict1 = {}

for a,b in zip(list1,list2):
    dict1[a] = b
    
print(dict1)
				
			

Output :

				
					{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}
				
			

get a list of odd and even keys from the dictionary

store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.