DS270223 | List | Dictionary I Set |Questions For Practice

Question 1:
Write a Python program to print all distinct values in a dictionary.

Sample Data : li = [{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]

Expected Output: {‘S005’, ‘S002’, ‘S007’, ‘S001’, ‘S009’}

Question 2:
Write a Python program to find the highest 2 values in a dictionary.

my_dict = {‘a’:500, ‘b’:5874, ‘c’: 560,‘d’:400, ‘e’:5880}

Expected Output : 5880, 5874

Question 3:
Write a Python program to combine two dictionary by adding values for common keys.
d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}

Expected Output, d3 = {‘a’: 400, ‘b’: 400, ‘d’: 400, ‘c’: 300}

Question 4:
Write a Python program to rotate a 3X3 matrix anti-clockwise.

Sample matrix : [[1,4,7],[2,5,8],[3,6,9]]
Expected Output matrix : [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

2 Likes

@Navin_Agrawal

Nice questions.

2 Likes

1.Write a Python program to print all distinct values in a dictionary.

d = [{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]

print("Original List: ",d)

r_value = set( val for dic in d for val in dic.values())

print("Unique Values: ",r_value)

1 Like

**Question 1:
Write a Python program to print all distinct values in a dictionary.

Sample Data : li = [{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]

Expected Output: {‘S005’, ‘S002’, ‘S007’, ‘S001’, ‘S009’}**
d =[{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]
newlist=[]
for i in d:
for j in i.values():
m.append(j)
print(set(newlist))

**Question 2:
Write a Python program to find the highest 2 values in a dictionary.

my_dict = {‘a’:500, ‘b’:5874, ‘c’: 560,‘d’:400, ‘e’:5880}
**

m=[]

for i in my_dict.values():

m.append(i)

m.sort()

print('higest value: ',m[-1])

print('higest value: ',m[-2])

Write a Python program to find the highest 2 values in a dictionary.

my_dict = {‘a’:500, ‘b’:5874, ‘c’: 560,‘d’:400, ‘e’:5880}
s= [[1,4,7],[2,5,8],[3,6,9]]
a=[]
b=[]
c=[]
for i in s:
a.append(i[0])
b.append(i[1])
c.append(i[2])
match=[a,b,c]
print(match)

Question 1:

Write a Python program to print all distinct values in a dictionary.

Sample Data : li = [{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]

Expected Output: {‘S005’, ‘S002’, ‘S007’, ‘S001’, ‘S009’}

list = [{“V”:“S001”} , {‘V’: ‘S002’}, {‘VI’: ‘S001’}, {‘VI’: ‘S005’}, {‘VII’:‘S005’}, {‘V’:‘S009’},{‘VIII’:‘S007’} ]
d= set()
for i in list:
for j in i.values():
d.add(j)
print(d)

Question 2:

Write a Python program to find the highest 2 values in a dictionary.

my_dict = {‘a’:500, ‘b’:5874, ‘c’: 560,‘d’:400, ‘e’:5880}

Expected Output : 5880, 5874

d = {‘a’:500, ‘b’:5874, ‘c’: 560,‘d’:400, ‘e’:5880}
l=[]
for i in d.values():
l.append(i)
x=sorted(l,reverse=True)
for i in range(0,2):
print(x[i] , end=‘,’)

Question 3:

Write a Python program to combine two dictionary by adding values for common keys.

d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}

d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}

Expected Output, d3 = {‘a’: 400, ‘b’: 400, ‘d’: 400, ‘c’: 300}

d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}
d3={}
for i in d1.keys():
if i in d2.keys():
d3[i]=d1[i] + d2[i]
else:
d3[i] = d1[i]

for j in d2.keys():
if j not in d3.keys():
d3[j] = d2[j]
print(d3)

Question 4:

Write a Python program to rotate a 3X3 matrix anti-clockwise.

Sample matrix : [[1,4,7],[2,5,8],[3,6,9]]

Expected Output matrix : [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

l=[[1,4,7],[2,5,8],[3,6,9]]
l1=[]
l2=[]
for i in range(0,len(l[0])):
for j in l:
l1.append(j[i])

print(l1)

[1, 2, 3, 4, 5, 6, 7, 8, 9] need help in this code …

li = [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},{"VIII":"S007"}]

distinct_values = set()

# iterate through each dictionary in the list
for d in li:
    # iterate through each value in the dictionary
    for v in d.values():
        # add the value to the distinct_values set
        distinct_values.add(v)

# print the distinct_values set
print(distinct_values)

Output:

{'S002', 'S009', 'S007', 'S001', 'S005'}

#1.Question 1:

#Write a Python program to print all distinct values in a dictionary.

L = [{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]

unique_value = set( val for dic in L for val in dic.values())

print("Unique Values: ",unique_value)

Write a Python program to combine two dictionary by adding values for common keys.
d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}

Ans:

from collections import Counter
my_dict1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
my_dict2 = {‘a’: 300, ‘b’: 200, ‘d’:400}
my_dict3 = Counter(my_dict1) + Counter(my_dict2)
print(my_dict3)

#Write a Python program to print all distinct values in a dictionary.
#Sample Data : li = [{“V”:“S001”}, {“V”: “S002”}, {“VI”: “S001”}, {“VI”: “S005”}, {“VII”:“S005”}, {“V”:“S009”},{“VIII”:“S007”}]
#Expected Output: {‘S005’, ‘S002’, ‘S007’, ‘S001’, ‘S009’}

list=[{“V”:“S001”},{“V”:“S002”},{“VI”:“S001”},{“VI”:“S005”},{“VII”:“S005”},{“V”:“S009”},{“VIII”:“S007”}]
d=set()
for i in list:
for j in i.values():
d.add(j)
print(d)

#2)Write a Python program to find the highest 2 values in a dictionary.
#my_dict = {‘a’:500, ‘b’:5874, ‘c’: 560,‘d’:400, ‘e’:5880}
#Expected Output : 5880, 5874
d={‘a’:500,‘b’:5874,‘c’:560,‘d’:400,‘e’:5880}
l=[]
for i in d.values():
l.append(i)
l.sort()
print(“First Highest Value:”,l[-1])
print(“Second Highest Value:”,l[-2])

#3)Write a Python program to combine two dictionary by adding values for common keys.
#d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
#d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}
#Expected Output, d3 = {‘a’: 400, ‘b’: 400, ‘d’: 400, ‘c’: 300}

d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}
d3 = dict(d1)
d3.update(d2)
for i,j in d1.items():
for x,y in d2.items():
if i==x:
d3[i]=(j+y)
print(d3)