Skip to content

Suggestion: Clarify dictionary access with square brackets, looping over keys, and data in the inventory in practices #1360

Description

@NathanLovato

A student was confused by this line in L25

var item_count = inventory[item_name]

They expected the square brackets to "return" the dictionary key's position in the list of keys, sort of similarly to how you access values in an array by index. The lesson and perhaps previous lessons don't make the distinction between array indexes and dictionary keys explicit enough.

The Lesson 25 practice also did not clearly explain how this modifies the inventory:

inventory[item_name] += amount

Things we could improve:

  • Review lessons probably 24 and 25 to clarify that [] is an access operator and it works differently depending on the context.
  • Explain that arrays use an int index, while dictionaries use a key to look up a value. We should probably say that dictionaries themselves do not have positional indexes (NB: unless you call a function to get an array of keys or values, but that's not the dictionary anymore).
  • Make sure we clearly explain and show that, in a loop over a dict, the loop variable contains one key at a time, and that using the brackets finds the value associated with that key. A visualization would help there and also mini exercises embedded in the lesson.

Related: #1217


Adding two more pieces of feedback:

Here should be more information that "inventory" is directly available. Or at least on the right site show the "inventory". I had to check the solution to understand, that the "inventory" is already available as a dictionary (and I don't have to create it)

Technical information:

  • Location: L24.P2 - Creating Dictionaries > Increasing item counts
  • File: res://course/lesson-25-creating-dictionaries/lesson.bbcode
  • Platform: Web
  • Commit: c26e748

Code from the current practice

func add_item(item_name, amount):
	inventory[item_name] += amount

And

It would help to explain the dictionary iteration in a bit more detail, especially for learners with some basic Python experience.

For example, a comparison with Python would make the concept much clearer.

for key, value in units.items()
place_unit(key, value)

Here, it is immediately apparent that key contains the position, the vector, and value contains the unit type, such as robot.

In the GDScript example.

for cell in units
var unit = units(cell)
place_unit(cell, unit)

It would help to explicitly explain that cell is just an arbitrarily chosen variable name. This can be confusing because cell has already been used with a different meaning earlier in the course. It could just as well be called key.

for key in units
var value = units(key)
place_unit(key, value)

When iterating over a Dictionary in GDScript, the loop variable receives the key. Unlike Pythons dictionary.items(), GDScript does not directly give us both the key and value. We therefore first get the key and then use that key to access the full Dictionary again, units(key), to retrieve the corresponding value.

This distinction would make the exercise considerably easier to understand, particularly for people with some prior Python knowledge.

Technical information:

  • Location: L25 - Looping over dictionaries
  • File: res://course/lesson-26-looping-over-dictionaries/lesson.bbcode
  • Platform: Web
  • Commit: c26e748

We should not base the explanation around Python given that most learners won't know it, but we can use that as a base to improve our explanations and examples of dictionaries.

Activity

  1. added
    contentIssues with the lessons, practices, including their code examples
    on Aug 3, 2026
  2. changed the title [-]Suggestion: Clarify dictionary access with square brackets[/-] [+]Suggestion: Clarify dictionary access with square brackets, looping over keys, and data in the inventory in practices[/+] on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    contentIssues with the lessons, practices, including their code examples

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions