Python 3D Academyবাংলা

Dictionaries in Python

Chapter 11: Dictionaries

5 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. Making dictionaries

    A dictionary stores key: value pairs, and d[key] opens the locker with that key and gives you its value.

  2. Changing a dictionary

    d[key] = value adds or changes a pair, del d[key] removes one, and get and pop read and remove without crashing on a missing key.

  3. Looping over a dictionary

    for key in d: visits every key in the order they were added, and d.items() hands you each key with its value.

  4. Counting with a dictionary

    To count things, start with an empty dictionary and, for each item, do counts[x] = counts.get(x, 0) + 1.

  5. Dictionaries in practice

    A dictionary's values can be lists or dictionaries, which lets you group items under a key and look things up in one step.