Bird
Raised Fist0
Intro to Computingfundamentals~10 mins

Dictionaries and key-value pairs in Intro to Computing - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a dictionary with a key 'name' and value 'Alice'.

Intro to Computing
person = [1]
Drag options to blanks, or click blank then click option'
A{'name': 'Alice'}
B['name', 'Alice']
C('name', 'Alice')
D'name': 'Alice'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] creates a list, not a dictionary.
Using parentheses () creates a tuple, not a dictionary.
Writing key-value without braces is invalid syntax.
2fill in blank
medium

Complete the code to access the value of the key 'age' from the dictionary.

Intro to Computing
age = person[1]
Drag options to blanks, or click blank then click option'
A.age
B(age)
C['age']
D{'age'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () tries to call a function, which is wrong here.
Using dot notation works for objects but not dictionaries.
Using curly braces {} is for creating dictionaries, not accessing values.
3fill in blank
hard

Fix the error in the code to add a new key 'city' with value 'Paris' to the dictionary.

Intro to Computing
person[1] = 'Paris'
Drag options to blanks, or click blank then click option'
A.city
B['city']
C(city)
D{city}
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation tries to access an attribute, which dictionaries don't support.
Using parentheses () or curly braces {} is invalid syntax here.
4fill in blank
hard

Complete the code to create a dictionary comprehension that maps each word to its length for words longer than 3 letters.

Intro to Computing
{word: len(word) for word in words if len(word) [1] 3}
Drag options to blanks, or click blank then click option'
A:
B>
C<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' causes syntax errors.
Using '<' instead of '>' changes the filter condition incorrectly.
5fill in blank
hard

Fill all three blanks to create a dictionary from a list of tuples, where keys are names in uppercase and values are ages greater than 20.

Intro to Computing
result = [1]: [2] for name, age in data if age [3] 20}
Drag options to blanks, or click blank then click option'
Aname.upper()
Bage
C>
Dname.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using name.lower() instead of uppercase changes keys incorrectly.
Using = instead of > causes syntax errors.
Swapping keys and values causes wrong dictionary structure.

Practice

(1/5)
1. What is a dictionary in computing fundamentals?
easy
A. A sequence of characters forming a word
B. A list of numbers sorted in order
C. A type of loop used for iteration
D. A collection of key-value pairs for storing data

Solution

  1. Step 1: Understand the definition of a dictionary

    A dictionary stores data as pairs where each key is linked to a value.
  2. Step 2: Compare with other data types

    Unlike lists or strings, dictionaries use keys to quickly find values.
  3. Final Answer:

    A collection of key-value pairs for storing data -> Option D
  4. Quick Check:

    Dictionary = key-value pairs [OK]
Hint: Remember: dictionary = keys + values [OK]
Common Mistakes:
  • Confusing dictionary with list or string
  • Thinking dictionary is a type of loop
  • Believing dictionary stores only numbers
2. Which of the following is the correct way to create a dictionary with keys 'name' and 'age'?
easy
A. ['name' = 'Alice', 'age' = 30]
B. {'name': 'Alice', 'age': 30}
C. {'name' -> 'Alice', 'age' -> 30}
D. ('name': 'Alice', 'age': 30)

Solution

  1. Step 1: Identify correct dictionary syntax

    Dictionaries use curly braces {} with key:value pairs separated by commas.
  2. Step 2: Check each option

    {'name': 'Alice', 'age': 30} uses correct syntax with colons and curly braces; others use wrong symbols or brackets.
  3. Final Answer:

    {'name': 'Alice', 'age': 30} -> Option B
  4. Quick Check:

    Dictionary syntax = curly braces + colons [OK]
Hint: Use curly braces and colons for dictionaries [OK]
Common Mistakes:
  • Using square brackets instead of curly braces
  • Using '=' or '->' instead of ':'
  • Using parentheses instead of braces
3. What will be the output of the following code?
person = {'name': 'Bob', 'age': 25}
print(person['age'])
medium
A. Bob
B. Error
C. 25
D. 'age'

Solution

  1. Step 1: Understand dictionary access by key

    Accessing person['age'] retrieves the value linked to key 'age'.
  2. Step 2: Identify the value for key 'age'

    The value stored is 25, so print(person['age']) outputs 25.
  3. Final Answer:

    25 -> Option C
  4. Quick Check:

    person['age'] = 25 [OK]
Hint: Keys fetch their values directly from dictionary [OK]
Common Mistakes:
  • Printing the key name instead of value
  • Confusing key with value
  • Expecting the whole dictionary to print
4. Identify the error in this dictionary code:
data = {'x': 10, 'y': 20, 'x': 30}
print(data['x'])
medium
A. Duplicate keys cause the last value to overwrite earlier ones
B. Syntax error due to missing comma
C. Keys must be numbers, not strings
D. Cannot print dictionary values directly

Solution

  1. Step 1: Check for duplicate keys in dictionary

    The key 'x' appears twice; dictionaries keep only the last value for duplicates.
  2. Step 2: Understand output of print statement

    Printing data['x'] shows 30, the last assigned value, overwriting 10.
  3. Final Answer:

    Duplicate keys cause the last value to overwrite earlier ones -> Option A
  4. Quick Check:

    Duplicate keys overwrite previous values [OK]
Hint: Duplicate keys keep only last value [OK]
Common Mistakes:
  • Thinking duplicate keys cause syntax error
  • Expecting both values to be stored
  • Believing keys must be numbers
5. Given a list of students and their scores:
students = [('Anna', 85), ('Ben', 92), ('Cara', 85), ('Dan', 92)]

Which dictionary comprehension creates a dictionary mapping each student to their score?
hard
A. {name: score for name, score in students}
B. {score: name for name, score in students}
C. {name: score if score > 90 else 0 for name, score in students}
D. {score: name if name.startswith('A') else 'Unknown' for name, score in students}

Solution

  1. Step 1: Understand the goal

    Create a dictionary with student names as keys and scores as values.
  2. Step 2: Analyze each comprehension

    {name: score for name, score in students} correctly maps name: score. {score: name for name, score in students} reverses keys and values, causing score keys to overwrite. Options C and D add conditions changing values or keys incorrectly.
  3. Final Answer:

    {name: score for name, score in students} -> Option A
  4. Quick Check:

    Keys = names, Values = scores [OK]
Hint: Keys are unique; use names as keys to avoid overwriting [OK]
Common Mistakes:
  • Using scores as keys causing overwrites
  • Adding conditions that change keys incorrectly
  • Confusing key-value order in comprehension