Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Practice Questions for Dictionary and sets

A. Basic Level (10 Questions)

Dictionary – Basic

1. Create and access

data = {"name": "Alice", "age": 25}
print(data["name"])

Output

Alice

2. Add a new key-value pair

student = {"id": 101}
student["marks"] = 90
print(student)

Output

{'id': 101, 'marks': 90}

3. Update a value

price = {"apple": 100}
price["apple"] = 120
print(price)

Output

{'apple': 120}

4. Get keys

d = {"a": 1, "b": 2}
print(d.keys())

Output

dict_keys(['a', 'b'])

5. Get values

d = {"x": 10, "y": 20}
print(d.values())

Output

dict_values([10, 20])

Set – Basic

6. Create a set

s = {1, 2, 3, 3}
print(s)

Output

{1, 2, 3}

7. Add element to set

s = {1, 2}
s.add(3)
print(s)

Output

{1, 2, 3}

8. Remove element

s = {1, 2, 3}
s.remove(2)
print(s)

Output

{1, 3}

9. Set length

s = {10, 20, 30}
print(len(s))

Output

3

10. Membership test

s = {5, 10, 15}
print(10 in s)

Output

True

B. Intermediate Level (10 Questions)

Dictionary – Intermediate

1. Iterate through dictionary

d = {"a": 1, "b": 2}
for k, v in d.items():
    print(k, v)

Output

a 1
b 2

2. Use get()

d = {"x": 10}
print(d.get("y", 0))

Output

0

3. Count frequency

data = ["a", "b", "a", "c", "b"]
freq = {}
for x in data:
    freq[x] = freq.get(x, 0) + 1
print(freq)

Output

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

4. Dictionary comprehension

nums = [1, 2, 3]
squares = {x: x*x for x in nums}
print(squares)

Output

{1: 1, 2: 4, 3: 9}

5. Merge dictionaries

d1 = {"a": 1}
d2 = {"b": 2}
d1.update(d2)
print(d1)

Output

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

Set – Intermediate

6. Union

a = {1, 2}
b = {2, 3}
print(a | b)

Output

{1, 2, 3}

7. Intersection

a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)

Output

{2, 3}

8. Difference

a = {1, 2, 3}
b = {2}
print(a - b)

Output

{1, 3}

9. Subset check

a = {1, 2}
b = {1, 2, 3}
print(a.issubset(b))

Output

True

10. Remove duplicates from list

lst = [1, 2, 2, 3]
unique = list(set(lst))
print(unique)

Output

[1, 2, 3]

C. Expert Level (5 Questions)

1. Invert a dictionary

d = {"a": 1, "b": 2, "c": 1}
inv = {}
for k, v in d.items():
    inv.setdefault(v, []).append(k)
print(inv)

Output

{1: ['a', 'c'], 2: ['b']}

2. Find common values across dictionaries

d1 = {"a": 1, "b": 2}
d2 = {"x": 2, "y": 3}
common = set(d1.values()) & set(d2.values())
print(common)

Output

{2}

3. Remove keys with duplicate values

d = {"a": 1, "b": 2, "c": 1}
seen = set()
result = {}
for k, v in d.items():
    if v not in seen:
        result[k] = v
        seen.add(v)
print(result)

Output

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

4. Group words by length

words = ["hi", "hello", "by", "python"]
group = {}
for w in words:
    group.setdefault(len(w), []).append(w)
print(group)

Output

{2: ['hi', 'by'], 5: ['hello'], 6: ['python']}

5. Find symmetric difference of multiple sets

sets = [{1, 2}, {2, 3}, {3, 4}]
result = sets[0]
for s in sets[1:]:
    result = result ^ s
print(result)

Output

{1, 4}

Daily-Life Based Advanced Questions (5)

Each problem reflects a realistic scenario, uses dictionary and/or set, and includes input + output code.


Q1. Student Attendance Tracker (Deduplication + Count)

Problem A teacher records roll numbers of students who entered the class multiple times. Remove duplicates and count total unique attendees.

attendance_log = [101, 102, 101, 103, 102, 104]

unique_students = set(attendance_log)
print("Unique students:", unique_students)
print("Total present:", len(unique_students))

Output

Unique students: {101, 102, 103, 104}
Total present: 4

Q2. Expense Categorization (Daily Spending)

Problem You track daily expenses and want total spending per category.

expenses = [
    ("food", 120),
    ("travel", 50),
    ("food", 80),
    ("shopping", 200),
    ("travel", 30)
]

summary = {}

for category, amount in expenses:
    summary[category] = summary.get(category, 0) + amount

print(summary)

Output

{'food': 200, 'travel': 80, 'shopping': 200}

Q3. Common Friends Between Two People (Social Media)

Problem Find mutual friends between two users.

alice_friends = {"Rahul", "Neha", "Amit", "Sneha"}
bob_friends = {"Amit", "Sneha", "Karan"}

mutual = alice_friends & bob_friends
print(mutual)

Output

{'Amit', 'Sneha'}

Q4. Product Purchase Frequency (E-commerce)

Problem An online store wants to know how many times each product was purchased.

orders = ["phone", "laptop", "phone", "tablet", "phone", "laptop"]

product_count = {}

for item in orders:
    product_count[item] = product_count.get(item, 0) + 1

print(product_count)

Output

{'phone': 3, 'laptop': 2, 'tablet': 1}

Q5. Identify Unique Visitors Across Multiple Days (Website Analytics)

Problem Each day’s website visitors are logged. Find total unique visitors.

day1 = {1, 2, 3, 4}
day2 = {3, 4, 5, 6}
day3 = {6, 7, 8}

all_visitors = day1 | day2 | day3
print("Unique visitors:", all_visitors)
print("Total visitors:", len(all_visitors))

Output

Unique visitors: {1, 2, 3, 4, 5, 6, 7, 8}
Total visitors: 8

Exercise 3

Create product_overlap(a, b) for two product sets and return their intersection.