Bird
Raised Fist0
Pythonprogramming~20 mins

Dictionary creation in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Dictionary Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of dictionary comprehension with condition
What is the output of this Python code?
Python
result = {x: x**2 for x in range(5) if x % 2 == 0}
print(result)
A{0: 0, 2: 4, 4: 16}
B{1: 1, 3: 9}
C{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Look at the condition after the for loop in the comprehension.
โ“ Predict Output
intermediate
2:00remaining
Output of dictionary from zip function
What does this code print?
Python
keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = dict(zip(keys, values))
print(result)
A{'a': 1, 'b': 2, 'c': 3}
B{1: 'a', 2: 'b', 3: 'c'}
CTypeError
D{('a', 1), ('b', 2), ('c', 3)}
Attempts:
2 left
๐Ÿ’ก Hint
zip pairs elements from two lists into tuples.
โ“ Predict Output
advanced
2:30remaining
Output of nested dictionary comprehension
What is the output of this code?
Python
result = {x: {y: x*y for y in range(3)} for x in range(2)}
print(result)
ASyntaxError
B{0: {0: 0, 1: 1, 2: 2}, 1: {0: 0, 1: 1, 2: 2}}
C{0: {0: 0, 1: 0, 2: 0}, 1: {0: 1, 1: 1, 2: 1}}
D{0: {0: 0, 1: 0, 2: 0}, 1: {0: 0, 1: 1, 2: 2}}
Attempts:
2 left
๐Ÿ’ก Hint
The inner dictionary multiplies x by y for y in 0 to 2.
โ“ Predict Output
advanced
2:30remaining
Output of dictionary creation with duplicate keys
What is the output of this code?
Python
result = {x % 3: x for x in range(6)}
print(result)
A{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
B{0: 0, 1: 1, 2: 2}
C{0: 3, 1: 4, 2: 5}
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Keys repeat because of modulo operation; later values overwrite earlier ones.
๐Ÿง  Conceptual
expert
2:00remaining
Error raised by invalid dictionary creation syntax
What error does this code raise?
Python
result = {x: x*2 if x > 2 for x in range(5)}
ATypeError
BSyntaxError
CKeyError
DNo error, outputs {3: 6, 4: 8}
Attempts:
2 left
๐Ÿ’ก Hint
Check the placement of the if condition in the dictionary comprehension.

Practice

(1/5)
1. Which of the following is the correct way to create an empty dictionary in Python?
easy
A. my_dict = ()
B. my_dict = []
C. my_dict = {}
D. my_dict = ''

Solution

  1. Step 1: Understand dictionary syntax

    Dictionaries in Python are created using curly braces {}.
  2. Step 2: Identify the empty dictionary

    An empty dictionary is represented as {}, not square brackets, parentheses, or quotes.
  3. Final Answer:

    my_dict = {} -> Option C
  4. Quick Check:

    Empty dictionary = {} [OK]
Hint: Use curly braces {} for dictionaries, brackets [] for lists [OK]
Common Mistakes:
  • Using [] which creates a list, not a dictionary
  • Using () which creates a tuple, not a dictionary
  • Using '' which creates an empty string
2. Which of the following is the correct syntax to create a dictionary with keys 'a' and 'b' and values 1 and 2 respectively?
easy
A. my_dict = ['a':1, 'b':2]
B. my_dict = {'a'=1, 'b'=2}
C. my_dict = ('a':1, 'b':2)
D. my_dict = {'a':1, 'b':2}

Solution

  1. Step 1: Check dictionary key-value pair syntax

    Dictionary pairs use colon : between key and value inside curly braces.
  2. Step 2: Identify correct syntax

    my_dict = {'a':1, 'b':2} uses curly braces and colons correctly. Options A and C use wrong brackets, and D uses equals sign which is invalid.
  3. Final Answer:

    my_dict = {'a':1, 'b':2} -> Option D
  4. Quick Check:

    Dictionary pairs use : inside {} [OK]
Hint: Use colons : between keys and values inside {} [OK]
Common Mistakes:
  • Using square brackets [] instead of curly braces {}
  • Using parentheses () instead of curly braces {}
  • Using equals sign = instead of colon :
3. What will be the output of the following code?
my_dict = {1: 'one', 2: 'two', 3: 'three'}
print(my_dict[2])
medium
A. 'two'
B. KeyError
C. 2
D. 'one'

Solution

  1. Step 1: Understand dictionary key lookup

    Accessing my_dict[2] retrieves the value for key 2.
  2. Step 2: Find value for key 2

    Key 2 maps to the string 'two' in the dictionary.
  3. Final Answer:

    'two' -> Option A
  4. Quick Check:

    my_dict[2] = 'two' [OK]
Hint: Dictionary[key] returns the value for that key [OK]
Common Mistakes:
  • Confusing keys and values
  • Expecting the key itself as output
  • Mistaking KeyError when key exists
4. The following code throws an error. What is the problem?
my_dict = {1: 'one', 2: 'two', 3: 'three'}
print(my_dict[4])
medium
A. Key 4 does not exist in the dictionary
B. Syntax error in dictionary creation
C. Values must be integers, not strings
D. Dictionary keys must be strings

Solution

  1. Step 1: Check dictionary keys

    The dictionary has keys 1, 2, and 3 only.
  2. Step 2: Accessing a missing key

    Trying to access my_dict[4] causes a KeyError because key 4 is not present.
  3. Final Answer:

    Key 4 does not exist in the dictionary -> Option A
  4. Quick Check:

    Accessing missing key causes KeyError [OK]
Hint: Check if key exists before accessing dictionary [OK]
Common Mistakes:
  • Assuming all keys exist
  • Confusing syntax error with runtime error
  • Thinking keys must be strings
5. You want to create a dictionary from two lists: keys = ['name', 'age', 'city'] and values = ['Alice', 30, 'NY']. Which code correctly creates this dictionary?
hard
A. my_dict = {keys: values}
B. my_dict = {keys[i]: values[i] for i in range(len(keys))}
C. my_dict = dict(keys, values)
D. my_dict = dict(zip(values, keys))

Solution

  1. Step 1: Understand dictionary creation from two lists

    We need to pair each key with its corresponding value by index.
  2. Step 2: Analyze options

    my_dict = {keys[i]: values[i] for i in range(len(keys))} uses dictionary comprehension with index to pair keys and values correctly. my_dict = dict(keys, values) is invalid syntax. my_dict = {keys: values} creates a dictionary with one key (the list) which is invalid. my_dict = dict(zip(values, keys)) reverses keys and values.
  3. Final Answer:

    my_dict = {keys[i]: values[i] for i in range(len(keys))} -> Option B
  4. Quick Check:

    Use dict comprehension with index to pair keys and values [OK]
Hint: Use dict comprehension with index or zip() to pair keys and values [OK]
Common Mistakes:
  • Using dict() with two lists directly
  • Swapping keys and values in zip()
  • Using list as a key