Create a python function which sorts Python Dictionary by Keys.
def sort_dict_by_keys():
size = int(input("Enter the size : "))
if size < 1:
print("Size must be a positive integer.")
return
dict = {}
for i in range(size):
key = input("Enter key: ")
value = input("Enter value: ")
dict[key] = value
sorted_dict = dict(sorted(dict.items()))
return sorted_dict
print("Invalid input. Please enter a valid integer for the size.")
sorted_dict = sort_dict_by_keys()
if sorted_dict:
print("Sorted Dictionary:")
for key, value in sorted_dict.items():
print(f"{key}: {value}")
1 Like
asciidict = dict()
def alfabet(num):
for i in num:
asciidict[chr(i)] = str(i)
alfabetTeller=range (97,123)
alfabet(alfabetTeller)
print(asciidict)
o/p:-
{'a': '97', 'b': '98', 'c': '99', 'd': '100', 'e': '101', 'f': '102', 'g': '103', 'h': '104', 'i': '105', 'j': '106', 'k': '107', 'l': '108', 'm': '109', 'n': '110', 'o': '111', 'p': '112', 'q': '113', 'r': '114', 's': '115', 't': '116', 'u': '117', 'v': '118', 'w': '119', 'x': '120', 'y': '121', 'z': '122'}
1 Like
def sort_mydict_by_keys():
num_size = int(input("Enter the size of the dictionary : "))
my_dict = {}
for i in range(num_size):
key = input("\nEnter the key : ")
value = input("Enter the value : ")
my_dict[key] = value
print("\nDictionary : ",my_dict)
sorted_value = sorted(my_dict.keys())
new_dict = {}
for i in sorted_value:
for key, value in my_dict.items():
if key == i:
new_dict[key] = value
print("sorted key Dictionary : ",new_dict)
sort_mydict_by_keys()
## my output
# Enter the size of the dictionary : 3
# Enter the key : tina
# Enter the value : 57
# Dictionary : {'tina': '57'}
# Enter the key : divya
# Enter the value : 73
# Dictionary : {'tina': '57', 'divya': '73'}
# Enter the key : komal
# Enter the value : 41
# Dictionary : {'tina': '57', 'divya': '73', 'komal': '41'}
# sorted key Dictionary : {'divya': '73', 'komal': '41', 'tina': '57'}
1 Like
Create a python function which sorts Python Dictionary by Keys.
def dict_sort():
dict1={}
size=int(input("Enter the size of the dictionary "))
for i in range(size):
key=int(input("Enter the key element "))
val=int(input("Enter the value "))
dict1[key]=val
print("The dictionary created is",dict1)
dict2={}
l1=list(dict1.keys())
l1.sort()
for i in l1:
dict2[i]=dict1[i]
print("The sorted dictionary is",dict2)
dict_sort()
1 Like
#Create a python function which sorts Python Dictionary by Keys.
def sort_dict():
size = int(input("Enter total elements to be in the dict:"))
print()
dict = {}
for i in range(size):
print("Enter",i+1, "key:")
key = int(input())
print("Enter",i+1, "value:")
value = int(input())
print()
dict[key] = value
print("The dictionary entered is:", dict)
sorted_dict = {}
lst = list(dict.keys())
lst.sort()
for i in lst:
sorted_dict[i] = dict[i]
print("Sorted dictionary:",sorted_dict)
sort_dict()
1 Like
my_dict = {}
num_pairs = int(input("Enter the number of key-value pairs: "))
for i in range(num_pairs):
key = input("Enter key: ")
value = input("Enter value: ")
my_dict[key] = value
print("Dictionary :", my_dict)
def sort_dict_by_keys(input_dict):
sorted_dict = dict(sorted(input_dict.items()))
return sorted_dict
sorted=sort_dict_by_keys(my_dict)
print(sorted)
without sorted function
def sort_dict_by_keys(input_dict):
keys = list(input_dict.keys())
keys.sort()
sorted_dict = {}
for key in keys:
sorted_dict[key] = input_dict[key]
return sorted_dict
1 Like
vij=dict()
for i in range(3):
char=input("enter alphabet:")
val=input("enter values:")
vij[char]=val
print(vij)
def item(x):
return vij[x]
sorted_keys = sorted(vij, key=item)
v = {}
for i in sorted_keys:
v[i] = vij[i]
print("Sorted Dictionary:\n",v)
# Create a python function which sorts Python Dictionary by Keys.
def dict_sort():
dict={}
size=int(input("Enter the size of the dict : "))
for i in range(size):
key=int(input("Enter the key :"))
val=int(input("Enter the value :"))
dict[key]=val
print("The dict :",dict)
dict1={}
lst=list(dict.keys())
lst.sort()
for i in lst:
dict1[i]=dict[i]
print("The sorted dict :",dict1)
dict_sort()