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.

Program to get a list of odd and even keys from the dictionary

In this python dictionary program, we will get a list of odd and even keys from the dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over the keys in the dictionary.
  3. Use an if-else statement separate odd and even keys from the dictionary.
  4. Print the output.
				
					dict1 = {1:25,5:'abc',8:'pqr',21:'xyz',12:'def',2:'utv'}

list1 = [[val,dict1[val]] for val in dict1 if val%2 == 0]
list2 = [[val,dict1[val]] for val in dict1 if val%2 != 0]

print("Even key = ",list1)
print("Odd key = ",list2)
				
			

Output :

				
					Even key =  [[8, 'pqr'], [12, 'def'], [2, 'utv']]
Odd key =  [[1, 25], [5, 'abc'], [21, 'xyz']]
				
			

concatenate two dictionaries

create a dictionary from two lists.

Program to concatenate two dictionaries

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

Steps to solve the program
  1. Take two dictionaries as input.
  2. Concatenate two dictionaries to form a single dictionary using update().
  3. Print the output.
				
					dict1 = {'Name':'Harry','Rollno':345,'Address':'Jordan'}
dict2 = {'Age':25,'salary': '$25k'}

dict1.update(dict2)

print(dict1)
				
			

Output :

				
					{'Name': 'Harry', 'Rollno': 345, 'Address': 'Jordan', 'Age': 25, 'salary': '$25k'}
				
			

move items from dict1 to dict2.

get a list of odd and even keys from the dictionary