Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Dictionary Practice

Python dictionaries — key-value maps for fast lookups and structured data.

1
Create a Dictionary from Keys and Values
Easy
Write a program that reads N keys and N values and creates a dictionary.
First line N. Second line N keys. Third line N values.
Print dict sorted by keys.
Sample Input 1
3
a b c
1 2 3
Sample Output 1
{'a': 1, 'b': 2, 'c': 3}
Sample Input 2
2
x y
10 20
Sample Output 2
{'x': 10, 'y': 20}
Sample Input 3
4
A B C D
100 200 300 400
Sample Output 3
{'A': 100, 'B': 200, 'C': 300, 'D': 400}
Sample Input 4
1
key
value
Sample Output 4
{'key': 'value'}
1 ≤ N ≤ 100
2
Access Dictionary Value by Key
Easy
Write a program to get the value for a given key. If key missing, print "Key not found".
First line N. Next N lines "key value". Last line the search key.
Print value or "Key not found".
Sample Input 1
3
a 1
b 2
c 3
b
Sample Output 1
2
Sample Input 2
2
name Alice
age 25
gender
Sample Output 2
Key not found
Sample Input 3
1
fruit apple
fruit
Sample Output 3
apple
Sample Input 4
3
x 10
y 20
z 30
y
Sample Output 4
20
1 ≤ N ≤ 100
3
Add or Update Key-Value Pair
Easy
Write a program to add a new key-value pair or update an existing one.
First line N. Next N lines "key value". Last line "new_key new_value".
Print updated dict sorted by keys.
Sample Input 1
2
a 1
b 2
c 3
Sample Output 1
{'a': 1, 'b': 2, 'c': 3}
Sample Input 2
1
name Alice
age 26
Sample Output 2
{'age': 26, 'name': 'Alice'}
Sample Input 3
3
x 100
y 200
z 300
w 400
Sample Output 3
{'w': 400, 'x': 100, 'y': 200, 'z': 300}
Sample Input 4
2
p 10
q 20
q 25
Sample Output 4
{'p': 10, 'q': 25}
1 ≤ N ≤ 100
4
Delete a Key from Dictionary
Easy
Write a program to delete a key from a dictionary.
First line N. Next N lines "key value". Last line the key to delete.
Print "Deleted" or "Key not found".
Sample Input 1
3
a 1
b 2
c 3
b
Sample Output 1
Deleted
Sample Input 2
2
name Alice
age 25
gender
Sample Output 2
Key not found
Sample Input 3
1
fruit apple
fruit
Sample Output 3
Deleted
Sample Input 4
4
x 10
y 20
z 30
w
w
Sample Output 4
Deleted
1 ≤ N ≤ 100
5
Check if Key Exists
Easy
Write a program to check if a given key exists in a dictionary.
First line N. Next N lines "key value". Last line lookup key.
Print "Yes" or "No".
Sample Input 1
3
a 1
b 2
c 3
b
Sample Output 1
Yes
Sample Input 2
2
name Alice
age 25
gender
Sample Output 2
No
Sample Input 3
1
key value
key
Sample Output 3
Yes
Sample Input 4
4
1 one
2 two
3 three
5
5
Sample Output 4
No
1 ≤ N ≤ 100
6
Count Number of Keys in Dictionary
Easy
Write a program to count and print the number of key-value pairs in a dictionary.
First line N. Next N lines "key value".
Print the count.
Sample Input 1
3
a 1
b 2
c 3
Sample Output 1
3
Sample Input 2
2
name Alice
age 25
Sample Output 2
2
Sample Input 3
1
key value
Sample Output 3
1
Sample Input 4
4
p 1
q 2
r 3
s 4
Sample Output 4
4
1 ≤ N ≤ 100
7
Get All Keys of a Dictionary
Easy
Write a program to print all keys of a dictionary as a list.
First line N. Next N lines "key value".
Print [key1, key2, ...].
Sample Input 1
3
a 1
b 2
c 3
Sample Output 1
[a, b, c]
Sample Input 2
2
name Alice
age 25
Sample Output 2
[name, age]
Sample Input 3
1
x 10
Sample Output 3
[x]
Sample Input 4
4
p 1
q 2
r 3
s 4
Sample Output 4
[p, q, r, s]
1 ≤ N ≤ 100
8
Get All Values of a Dictionary
Easy
Write a program to print all values of a dictionary as a list.
First line N. Next N lines "key value".
Print [val1, val2, ...].
Sample Input 1
3
a 1
b 2
c 3
Sample Output 1
[1, 2, 3]
Sample Input 2
2
name Alice
age 25
Sample Output 2
[Alice, 25]
Sample Input 3
1
x 10
Sample Output 3
[10]
Sample Input 4
4
a apple
b banana
c cherry
d date
Sample Output 4
[apple, banana, cherry, date]
1 ≤ N ≤ 100
9
Merge Two Dictionaries
Easy
Write a program to merge two dictionaries. Overlapping keys use the second dict's value.
First line N1. Lines "key value" for dict1. Then N2. Lines for dict2.
Print merged dict sorted by keys.
Sample Input 1
2
a 1
b 2
2
a 10
c 30
Sample Output 1
{'a': 10, 'b': 2, 'c': 30}
Sample Input 2
1
x 0
3
x 100
y 200
z 300
Sample Output 2
{'x': 100, 'y': 200, 'z': 300}
Sample Input 3
3
p 1
q 2
r 3
0
Sample Output 3
{'p': 1, 'q': 2, 'r': 3}
Sample Input 4
2
name Alice
age 25
3
name Bob
age 30
city LA
Sample Output 4
{'age': 30, 'city': 'LA', 'name': 'Bob'}
1 ≤ N1, N2 ≤ 50
10
Dictionary Comprehension — Squares
Easy
Write a program to create a dict mapping 1..N to their squares.
First line contains N (1 ≤ N ≤ 20).
Print the dictionary.
Sample Input 1
5
Sample Output 1
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Sample Input 2
3
Sample Output 2
{1: 1, 2: 4, 3: 9}
Sample Input 3
1
Sample Output 3
{1: 1}
Sample Input 4
7
Sample Output 4
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
1 ≤ N ≤ 20
1
Frequency Count Using Dictionary
Medium
Write a program to count frequency of each element in a list using a dict.
First line N. Second line N integers.
Print "elem: count" sorted by element.
Sample Input 1
8
1 2 2 3 2 1 3 4
Sample Output 1
1: 3
2: 2
3: 2
4: 1
Sample Input 2
5
5 5 5 5 5
Sample Output 2
5: 5
Sample Input 3
4
a b a c
Sample Output 3
a: 2
b: 1
c: 1
Sample Input 4
6
1 -2 0 -2 1 -1
Sample Output 4
-2: 2
-1: 1
0: 1
1: 2
1 ≤ N ≤ 100
2
Invert a Dictionary (Swap Keys and Values)
Medium
Write a program to invert a dictionary — swap keys and values.
First line N. Next N lines "key value". Values are unique.
Print inverted dict sorted by new keys.
Sample Input 1
3
a 1
b 2
c 3
Sample Output 1
{1: 'a', 2: 'b', 3: 'c'}
Sample Input 2
2
x 10
y 20
Sample Output 2
{10: 'x', 20: 'y'}
Sample Input 3
1
key val
Sample Output 3
{'val': 'key'}
Sample Input 4
3
color red
size big
texture soft
Sample Output 4
{'big': 'size', 'red': 'color', 'soft': 'texture'}
1 ≤ N ≤ 100
3
Find Key with Maximum Value
Medium
Write a program to find the key with the maximum value in a dict.
First line N. Next N lines "key value".
Print key with highest value (lexicographically smallest if tie).
Sample Input 1
3
a 10
b 25
c 30
Sample Output 1
c
Sample Input 2
4
a 50
b 50
c 40
d 10
Sample Output 2
a
Sample Input 3
2
small -5
large -10
Sample Output 3
small
Sample Input 4
1
only 42
Sample Output 4
only
1 ≤ N ≤ 100
4
Filter Dictionary by Value Threshold
Medium
Write a program to filter a dict, keeping entries where value > T.
First line N. Next N lines "key value". Last line T.
Print filtered dict sorted by keys.
Sample Input 1
4
a 10
b 20
c 30
d 25
15
Sample Output 1
{'b': 20, 'c': 30, 'd': 25}
Sample Input 2
3
x 1
y 2
z 3
5
Sample Output 2
{}
Sample Input 3
3
a 0
b 100
c 200
50
Sample Output 3
{'b': 100, 'c': 200}
Sample Input 4
2
score1 85
score3 92
80
Sample Output 4
{'score1': 85, 'score3': 92}
1 ≤ N ≤ 100
5
Word Length Dictionary
Medium
Write a program to read a sentence and map each word to its length in a dict.
A single line containing a sentence.
Print dict sorted by keys.
Sample Input 1
hello world python
Sample Output 1
{'hello': 5, 'python': 6, 'world': 5}
Sample Input 2
a bb ccc dddd
Sample Output 2
{'a': 1, 'bb': 2, 'ccc': 3, 'dddd': 4}
Sample Input 3
one two three
Sample Output 3
{'one': 3, 'three': 5, 'two': 3}
Sample Input 4
hi
Sample Output 4
{'hi': 2}
Length ≤ 500
6
Group Words by First Letter
Medium
Write a program to group words by their first letter using a dict.
First line N. Second line N space-separated words.
Print "letter: words" per line, sorted by letter.
Sample Input 1
6
apple ant banana cherry cat
Sample Output 1
a: apple ant
b: banana
c: cherry cat
Sample Input 2
4
dog deer elephant
Sample Output 2
d: dog deer
e: elephant
Sample Input 3
3
go got gone
Sample Output 3
g: go got gone
Sample Input 4
5
python pearl ruby rust
Sample Output 4
p: python pearl
r: ruby rust
1 ≤ N ≤ 100
7
Access Nested Dictionary Value
Medium
Write a program to access a value in a nested dict given a key path.
First line D. Second line D keys. Dict: {"a":{"b":30}, "name":"Alice", "outer":{"inner":42}}.
Print value or "Key not found".
Sample Input 1
2
a b
Sample Output 1
30
Sample Input 2
1
name
Sample Output 2
Alice
Sample Input 3
3
x y z
Sample Output 3
Key not found
Sample Input 4
2
outer inner
Sample Output 4
42
1 ≤ D ≤ 5
8
Dictionary of Even/Odd Squares
Medium
Write a program to separate squares of 1..N into "even" and "odd" lists.
First line N (1 ≤ N ≤ 20).
Print {'even': [...], 'odd': [...]}.
Sample Input 1
5
Sample Output 1
{'even': [4, 16], 'odd': [1, 9, 25]}
Sample Input 2
3
Sample Output 2
{'even': [4], 'odd': [1, 9]}
Sample Input 3
1
Sample Output 3
{'even': [], 'odd': [1]}
Sample Input 4
6
Sample Output 4
{'even': [4, 16, 36], 'odd': [1, 9, 25]}
1 ≤ N ≤ 20
9
Default Value with get()
Medium
Write a program to safely access dict values with get(key, default).
First line N. Lines "key value". Last line "lookup_key default_val".
Print value if exists, else default.
Sample Input 1
3
a 10
b 20
c 30
b 0
Sample Output 1
20
Sample Input 2
2
name Alice
age 25
gender Default
Sample Output 2
Default
Sample Input 3
1
x 100
x 200
Sample Output 3
200
Sample Input 4
4
p 1
q 2
r 3
s 4
z Not found
Sample Output 4
Not found
1 ≤ N ≤ 100
10
Sort Dictionary by Values
Medium
Write a program to sort a dictionary by values ascending.
First line N. Next N lines "key value" (int).
Print "key: value" per line sorted by value.
Sample Input 1
4
a 10
b 25
c 20
d 15
Sample Output 1
a: 10
d: 15
c: 20
b: 25
Sample Input 2
3
x 10
y 5
z 1
Sample Output 2
z: 1
y: 5
x: 10
Sample Input 3
2
large 1000
small -100
Sample Output 3
small: -100
large: 1000
Sample Input 4
5
a 6
b 5
c 4
d 3
e 2
Sample Output 4
e: 2
d: 3
c: 4
b: 5
a: 6
1 ≤ N ≤ 100
1
Dictionary of Lists — Group by Category
Hard
Write a program to group integers into even and odd categories using a dict of lists.
First line contains N. Second line contains N integers.
Print dict with keys "even" and "odd" mapping to sorted lists.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
{'even': [2, 4], 'odd': [1, 3, 5]}
Sample Input 2
4
10 15 20 25
Sample Output 2
{'even': [10, 20], 'odd': [15, 25]}
Sample Input 3
3
0 1 2
Sample Output 3
{'even': [0, 2], 'odd': [1]}
Sample Input 4
6
7 6 5 4 3 2
Sample Output 4
{'even': [2, 4, 6], 'odd': [3, 5, 7]}
1 ≤ N ≤ 100
2
Character Frequency in String
Hard
Write a program to count the frequency of each character in a string using a dictionary.
A single line containing a string.
Print "char: count" sorted by character, one per line.
Sample Input 1
hello
Sample Output 1
e: 1
h: 1
l: 2
o: 1
Sample Input 2
aabb
Sample Output 2
a: 2
b: 2
Sample Input 3
abc
Sample Output 3
a: 1
b: 1
c: 1
Sample Input 4
aaaa
Sample Output 4
a: 4
String length ≤ 500
3
Dictionary of Squares (1 to N)
Hard
Write a program to create a dict mapping integers to their squares for numbers 1 to N.
First line contains N (1 ≤ N ≤ 50).
Print the dictionary.
Sample Input 1
5
Sample Output 1
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Sample Input 2
3
Sample Output 2
{1: 1, 2: 4, 3: 9}
Sample Input 3
1
Sample Output 3
{1: 1}
Sample Input 4
10
Sample Output 4
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
1 ≤ N ≤ 50
4
Statistics Dictionary
Hard
Write a program to compute mean, median, and mode of a list of integers and store them in a dict.
First line contains N. Second line contains N integers.
Print dict with keys "mean", "median", "mode".
Sample Input 1
5
1 2 3 4 5
Sample Output 1
{'mean': 3.0, 'median': 3, 'mode': 1}
Sample Input 2
6
1 2 2 3 3 4
Sample Output 2
{'mean': 2.5, 'median': 2.5, 'mode': 2}
Sample Input 3
3
5 5 5
Sample Output 3
{'mean': 5.0, 'median': 5, 'mode': 5}
Sample Input 4
4
1 1 2 4
Sample Output 4
{'mean': 2.0, 'median': 1.5, 'mode': 1}
1 ≤ N ≤ 100
5
Dictionary from Two Lists — Zip
Hard
Write a program to create a dictionary by zipping two lists (keys and values). If lengths differ, extra elements are ignored.
First line N keys. Second line N keys. Third line M values. Fourth line M values.
Print the zipped dictionary sorted by keys.
Sample Input 1
3
a b c
3
1 2 3
Sample Output 1
{'a': 1, 'b': 2, 'c': 3}
Sample Input 2
2
x y
3
10 20 30
Sample Output 2
{'x': 10, 'y': 20}
Sample Input 3
4
p q r s
2
100 200
Sample Output 3
{'p': 100, 'q': 200}
Sample Input 4
1
k
3
a b c
Sample Output 4
{'k': 'a'}
1 ≤ N, M ≤ 100
6
Nested Dictionary — Student Grades
Hard
Write a program to create a nested dict: {"student_name": {"subject": score}}. Given student names, subjects, and scores, build the structure.
First line N (number of records). Next N lines: "name subject score".
Print the nested dictionary sorted by student name and subject.
Sample Input 1
3
Alice Math 90
Bob Science 85
Alice Science 95
Sample Output 1
{'Alice': {'Math': 90, 'Science': 95}, 'Bob': {'Science': 85}}
Sample Input 2
1
John Eng 80
Sample Output 2
{'John': {'Eng': 80}}
Sample Input 3
2
A X 10
B Y 20
Sample Output 3
{'A': {'X': 10}, 'B': {'Y': 20}}
Sample Input 4
2
X M 100
X S 200
Sample Output 4
{'X': {'M': 100, 'S': 200}}
1 ≤ N ≤ 100
7
Dictionary Difference
Hard
Write a program to find the difference between two dictionaries (keys in first but not in second), with their values.
First line N1 (dict1). N1 lines "key value". Then N2 (dict2). N2 lines "key value".
Print the difference dict sorted by keys. If none, print "{}".
Sample Input 1
3
a 1
b 2
c 3
1
b 2
Sample Output 1
{'a': 1, 'c': 3}
Sample Input 2
2
x 10
y 20
2
x 10
z 30
Sample Output 2
{'y': 20}
Sample Input 3
2
p 100
q 200
2
p 100
q 200
Sample Output 3
{}
Sample Input 4
3
m 5
n 10
o 15
0
Sample Output 4
{'m': 5, 'n': 10, 'o': 15}
1 ≤ N1, N2 ≤ 100
8
Cumulative Frequency Dictionary
Hard
Write a program to create a cumulative frequency dictionary from a list of integers — each key is an integer and its value is the count of elements ≤ that integer.
First line contains N. Second line contains N integers.
Print cumulative frequency dict sorted by keys.
Sample Input 1
5
1 2 2 3 4
Sample Output 1
{1: 1, 2: 3, 3: 4, 4: 5}
Sample Input 2
3
5 5 5
Sample Output 2
{5: 3}
Sample Input 3
4
10 20 10 30
Sample Output 3
{10: 2, 20: 3, 30: 4}
Sample Input 4
6
-2 -1 0 1 2 3
Sample Output 4
{-2: 1, -1: 2, 0: 3, 1: 4, 2: 5, 3: 6}
1 ≤ N ≤ 100
9
Key with Maximum Value Length
Hard
Write a program to find the key whose corresponding value (a string) has the maximum length. If tied, pick the lexicographically smallest key.
First line N. Next N lines "key value" (value is a string).
Print the key with longest value.
Sample Input 1
3
short ab
medium abcdef
long abc
Sample Output 1
medium
Sample Input 2
2
a hello
b hi
Sample Output 2
a
Sample Input 3
1
only hello
Sample Output 3
only
Sample Input 4
4
x cat
y elephant
z dog
w tiger
Sample Output 4
y
1 ≤ N ≤ 100
10
Invert Dictionary with Lists (Non-Unique Values)
Hard
Write a program to invert a dictionary where values are not unique. The inverted dict maps each value to a list of keys that had that value.
First line N. Next N lines "key value".
Print inverted dict sorted by keys (new keys = old values).
Sample Input 1
3
a 1
b 2
c 1
Sample Output 1
{1: ['a', 'c'], 2: ['b']}
Sample Input 2
2
x 10
y 10
Sample Output 2
{10: ['x', 'y']}
Sample Input 3
4
p 5
q 5
r 5
s 10
Sample Output 3
{5: ['p', 'q', 'r'], 10: ['s']}
Sample Input 4
1
k v
Sample Output 4
{'v': ['k']}
1 ≤ N ≤ 100