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.
Input Format
First line N. Second line N keys. Third line N values.
Output Format
Print dict sorted by keys.
Sample Test Cases
|
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'}
|
Constraints
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".
Input Format
First line N. Next N lines "key value". Last line the search key.
Output Format
Print value or "Key not found".
Sample Test Cases
|
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 |
Constraints
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.
Input Format
First line N. Next N lines "key value". Last line "new_key new_value".
Output Format
Print updated dict sorted by keys.
Sample Test Cases
|
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}
|
Constraints
1 ≤ N ≤ 100
4
Delete a Key from Dictionary
Easy
Write a program to delete a key from a dictionary.
Input Format
First line N. Next N lines "key value". Last line the key to delete.
Output Format
Print "Deleted" or "Key not found".
Sample Test Cases
|
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 |
Constraints
1 ≤ N ≤ 100
5
Check if Key Exists
Easy
Write a program to check if a given key exists in a dictionary.
Input Format
First line N. Next N lines "key value". Last line lookup key.
Output Format
Print "Yes" or "No".
Sample Test Cases
|
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 |
Constraints
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.
Input Format
First line N. Next N lines "key value".
Output Format
Print the count.
Sample Test Cases
|
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 |
Constraints
1 ≤ N ≤ 100
7
Get All Keys of a Dictionary
Easy
Write a program to print all keys of a dictionary as a list.
Input Format
First line N. Next N lines "key value".
Output Format
Print [key1, key2, ...].
Sample Test Cases
|
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] |
Constraints
1 ≤ N ≤ 100
8
Get All Values of a Dictionary
Easy
Write a program to print all values of a dictionary as a list.
Input Format
First line N. Next N lines "key value".
Output Format
Print [val1, val2, ...].
Sample Test Cases
|
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] |
Constraints
1 ≤ N ≤ 100
9
Merge Two Dictionaries
Easy
Write a program to merge two dictionaries. Overlapping keys use the second dict's value.
Input Format
First line N1. Lines "key value" for dict1. Then N2. Lines for dict2.
Output Format
Print merged dict sorted by keys.
Sample Test Cases
|
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'}
|
Constraints
1 ≤ N1, N2 ≤ 50
10
Dictionary Comprehension — Squares
Easy
Write a program to create a dict mapping 1..N to their squares.
Input Format
First line contains N (1 ≤ N ≤ 20).
Output Format
Print the dictionary.
Sample Test Cases
|
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}
|
Constraints
1 ≤ N ≤ 20
1
Frequency Count Using Dictionary
Medium
Write a program to count frequency of each element in a list using a dict.
Input Format
First line N. Second line N integers.
Output Format
Print "elem: count" sorted by element.
Sample Test Cases
|
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 |
Constraints
1 ≤ N ≤ 100
2
Invert a Dictionary (Swap Keys and Values)
Medium
Write a program to invert a dictionary — swap keys and values.
Input Format
First line N. Next N lines "key value". Values are unique.
Output Format
Print inverted dict sorted by new keys.
Sample Test Cases
|
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'}
|
Constraints
1 ≤ N ≤ 100
3
Find Key with Maximum Value
Medium
Write a program to find the key with the maximum value in a dict.
Input Format
First line N. Next N lines "key value".
Output Format
Print key with highest value (lexicographically smallest if tie).
Sample Test Cases
|
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 |
Constraints
1 ≤ N ≤ 100
4
Filter Dictionary by Value Threshold
Medium
Write a program to filter a dict, keeping entries where value > T.
Input Format
First line N. Next N lines "key value". Last line T.
Output Format
Print filtered dict sorted by keys.
Sample Test Cases
|
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}
|
Constraints
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.
Input Format
A single line containing a sentence.
Output Format
Print dict sorted by keys.
Sample Test Cases
|
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}
|
Constraints
Length ≤ 500
6
Group Words by First Letter
Medium
Write a program to group words by their first letter using a dict.
Input Format
First line N. Second line N space-separated words.
Output Format
Print "letter: words" per line, sorted by letter.
Sample Test Cases
|
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 |
Constraints
1 ≤ N ≤ 100
7
Access Nested Dictionary Value
Medium
Write a program to access a value in a nested dict given a key path.
Input Format
First line D. Second line D keys. Dict: {"a":{"b":30}, "name":"Alice", "outer":{"inner":42}}.
Output Format
Print value or "Key not found".
Sample Test Cases
|
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 |
Constraints
1 ≤ D ≤ 5
8
Dictionary of Even/Odd Squares
Medium
Write a program to separate squares of 1..N into "even" and "odd" lists.
Input Format
First line N (1 ≤ N ≤ 20).
Output Format
Print {'even': [...], 'odd': [...]}.
Sample Test Cases
|
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]}
|
Constraints
1 ≤ N ≤ 20
9
Default Value with get()
Medium
Write a program to safely access dict values with get(key, default).
Input Format
First line N. Lines "key value". Last line "lookup_key default_val".
Output Format
Print value if exists, else default.
Sample Test Cases
|
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 |
Constraints
1 ≤ N ≤ 100
10
Sort Dictionary by Values
Medium
Write a program to sort a dictionary by values ascending.
Input Format
First line N. Next N lines "key value" (int).
Output Format
Print "key: value" per line sorted by value.
Sample Test Cases
|
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 |
Constraints
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.
Input Format
First line contains N. Second line contains N integers.
Output Format
Print dict with keys "even" and "odd" mapping to sorted lists.
Sample Test Cases
|
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]}
|
Constraints
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.
Input Format
A single line containing a string.
Output Format
Print "char: count" sorted by character, one per line.
Sample Test Cases
|
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 |
Constraints
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.
Input Format
First line contains N (1 ≤ N ≤ 50).
Output Format
Print the dictionary.
Sample Test Cases
|
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}
|
Constraints
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.
Input Format
First line contains N. Second line contains N integers.
Output Format
Print dict with keys "mean", "median", "mode".
Sample Test Cases
|
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}
|
Constraints
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.
Input Format
First line N keys. Second line N keys. Third line M values. Fourth line M values.
Output Format
Print the zipped dictionary sorted by keys.
Sample Test Cases
|
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'}
|
Constraints
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.
Input Format
First line N (number of records). Next N lines: "name subject score".
Output Format
Print the nested dictionary sorted by student name and subject.
Sample Test Cases
|
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}}
|
Constraints
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.
Input Format
First line N1 (dict1). N1 lines "key value". Then N2 (dict2). N2 lines "key value".
Output Format
Print the difference dict sorted by keys. If none, print "{}".
Sample Test Cases
|
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}
|
Constraints
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.
Input Format
First line contains N. Second line contains N integers.
Output Format
Print cumulative frequency dict sorted by keys.
Sample Test Cases
|
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}
|
Constraints
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.
Input Format
First line N. Next N lines "key value" (value is a string).
Output Format
Print the key with longest value.
Sample Test Cases
|
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 |
Constraints
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.
Input Format
First line N. Next N lines "key value".
Output Format
Print inverted dict sorted by keys (new keys = old values).
Sample Test Cases
|
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']}
|
Constraints
1 ≤ N ≤ 100