Dict Practice Question ( DS140823 ) - 08-09-2023

  1. Write a program to get a value from user and check if that value is present as key inside dict or not. (Dynamic Dict)

  2. Write a program to take a text from user and find the count of frequency of each word and store it inside dictionary.

Sample Input : 
This is a sample text. This text contains sample words.

Sample Output : 
{'this': 2, 'is': 1, 'a': 1, 'sample': 2, 'text': 2, 'contains': 1, 'words': 1}
2 Likes
1.
size = int(input("Enter the size of the dictionary: "))
dict = {}
for i in range(size):
    key = input("Enter the key : ")
    value = input("Enter the Value : ")
    dict[key] = value
element = input("Enter a value to check if it's a key in the dictionary: ")
if element in dict:
    print("is present as a key in the dictionary.")
else:
    print("is not present as a key in the dictionary.")




2.
text = input("Enter a text: ")
words = text.split()
word_count = {}
for word in words:
    if word in word_count:
        word_count[word] += 1
    else:
        word_count[word] = 1
print(word_count)

1 Like

Write a program to get a value from user and check if that value is present as key inside dict or not. (Dynamic Dict)

size=int(input("Enter size of the dictionary"))
dict={}
for i in range(size):
	keys=int(input("Enter a key "))
	value=int(input("Enter the value"))
	dict[keys]=value
print(dict)
key_find=int(input("Please Enter a value to check in dictionary"))
for i in dict:
	if i == key_find:
		print(key_find," is present in the dictionary as Key")
		break
else:
	print(key_find,"is not present in the dictionary as Key")

Write a program to take a text from user and find the count of frequency of each word and store it inside dictionary.

str1=input("Please enter a string ")
lst1=str1.split()
lst2=[]
dict1={}
for i in lst1:
	lst2.append(lst1.count(i))
for i in range(len(lst1)):
	dict1[lst1[i]]=lst2[i]
print(dict1)
1 Like
#writ a program to get a value from user and check if that value is present as key insid dict or not
size=int(input("enter no.of elements entered into dectionary"))
dict1={}
for i in range(size):
    key=int(input("enter key value"))
    value=(input("enter value"))
    dict1[key]=value
print(dict1)
key_search=int(input("enter element to be searched"))
if key_search in dict:
       print("key is found",key_search)
else:
   print("key is not found",key_search)
output:-enter no.of elements entered into dectionary2
enter key value1
enter valuemama
enter key value2
enter valuesai
{1: 'mama', 2: 'sai'}
enter element to be searched2
key is found 2
1 Like
1.

my_dict = {}
size = int(input("Enter the number of key-value pairs: "))

for i in range(size):
    key = input("Enter the key: ")
    value = input("Enter the value for key: ")
    my_dict[key] = value
print("The created dictionary: ",my_dict)

user_ip = input("Enter the value to be checked: ")
for i in my_dict.values():
  if i == user_ip:
     print("The value IS present!")
     break
else:
 print("The value is NOT present!")


2.

user_ip = input("Enter a text: ")
text = user_ip.split()
count = {}
for word in text:
    if word in count:
        count[word] += 1
    else:
        count[word] = 1
print(count)



1 Like

1. Write a program to take a text from user and find the count of frequency of each word and store it inside dictionary.

string = input("Enter the string :")
my_list=[]
my_list=string.split()
word_freq=[my_list.count(p) for p in my_list]
print("The frequency of words is ...")
print(dict(zip(my_list,word_freq)))
output:-
Enter the string :hello how are you hello
The frequency of words is ...
{'hello': 2, 'how': 1, 'are': 1, 'you': 1}
# 1] Write a program to get a value from user and check if that value is present as key inside dict or not. (Dynamic Dict)

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("\nMy Dictionary : ",my_dict)

find_key = input("Enter your key which you are searching for : ")
if find_key in my_dict:
    print("\nThe",find_key,"key is present in dictionary.")
else :
    print("\nThe",find_key,"key is not present in dictionary.")

# 2] Write a program to take a text from user and find the count of frequency of each word and store it inside dictionary.

#Sample Input : This is a sample text. This text contains sample words.

#Sample Output : {'this': 2, 'is': 1, 'a': 1, 'sample': 2, 'text': 2, 'contains': 1, 'words': 1}

string = input("Please enter any String : ")
words = []

words = string.split()
myDict = {}
for key in words:
    myDict[key] = words.count(key)

print("Dictionary Items  :  ", myDict)
1 Like

Ans 1-

size = int(input("Enter the size of dict: "))
my_dict = {}
for i in range(size):
    key = input("Enter the key :")
    value = input("Enter the value :")
    my_dict[key] = value
print(my_dict)
value = input("Enter the value :")
if value in my_dict:
    print("Value is present as key inside dict.")
else:
    print("Value is not present as key inside dict.")

Ans 2 -

text = input("Enter any text:")
text_1 = text.split()
dict = {}
for word in text_1:
    if word in dict:
        dict[word] += 1
    else:
        dict[word] = 1
print(dict)
1 Like

#1. Write a program to get a value from user and check if that value is present as key inside dict or not. (Dynamic 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 = input()
    print("Enter",i+1, "value:")
    value = input()
    print()
    dict[key] = value

print("The dictionary is:", dict)

check_key = input("Enter the key to check :")
print()
if check_key in dict:
    print("The key",check_key , " is present in dict")
else:
    print("The key", check_key, "is not present")

2. Write a program to take a text from user and find the count of frequency of each word and store it inside dictionary.

string = input("Enter string:")
print()
lst = string.split()
dict = {}
for key in lst:
    if string in dict:
        dict[key] += 1
    else:
        dict[key] = 1   
print("word count", dict)
1 Like
# Write a program to get a value from user and check if that value is present as key inside dict or not. (Dynamic Dict)

size=int(input("enter size of dict :"))
dict={}
for i in range(size):
    key=input("enter key :")
    value=input("enter value :")
    dict[key]=value
element=input("enter value to check :")
if element in dict:
    print("key is present")
else:
    print("key is not present")



#Write a program to take a text from user and find the count of frequency of each word and store it inside dictionary.

userip = input("Enter a text: ") 
text = userip.split()
count = {}
for word in text:
    if word in count:
        count[word] += 1
    else:
        count[word] = 1
print(count)