Introduction

Author

Clayton Cafiero

Published

2025-06-23

So far, the data structures we’ve seen are either flat, one-dimensional objects—lists, tuples—or perhaps lists containing lists, lists containing tuples, and so on. However, in many instances it makes sense to structure our data in different ways. In this chapter, we’ll learn about dictionaries, sets, named tuples, and a structured data format known as JSON (pronounced “Jay-son”).

Giving structure to our data helps us organize things into logical components. This makes it easier and safer to find, read, write, and update data with our programs.

There are other benefits as well: with dictionaries, we can refer to elements by name (called a key), or in the case of named tuples, we can refer to elements by their field name. In either case, the concept is similar.

There are many programming problems that are best solved with structured data. Structured data is a first step toward understanding classes and encapsulation. These are fundamental concepts in what is called object-oriented programming (OOP), a commonly-used programming paradigm with many applications.

Dictionaries are ubiquitous, no doubt due to their usefulness and flexibility. Dictionaries store information in key/value pairs—we look up a value in a dictionary by its key. In this chapter we’ll learn about dictionaries: how to create them, modify them, iterate over them and so on.

Sets are similar but not identical to sets you might have seen in a mathematics course. Like mathematical sets, they are unordered and do not contain duplicate elements. Unlike mathematical sets, Python sets must, of necessity, be finite.

Named tuples are like tuples, but we can refer to the elements of a named tuple by name or by index—either works. Like tuples, named tuples are immutable. This can make our code more readable and easier to reason about.

We’ll also see JSON. JSON is an acronym for “JavaScript object notation.” JSON is a widely used format for data interchange and many APIs (application program interfaces) for web services return data in JSON format. Don’t be fooled by the name. Though originally conceived as a way of notating objects in JavaScript, JSON escaped the lab so to speak, and JavaScript isn’t required to work with JSON data. Indeed, Python provides its own JSON module that includes tools for reading and writing JSON data, and converting to native Python datastructures.

Learning objectives

  • You will learn how to create a dictionary, set, or named tuple.
  • You will understand that dictionaries and sets are mutable, meaning that their contents may change.
  • You will learn how to access individual values in a dictionary by keys and within named tuples by field name.
  • You will learn how to iterate over dictionaries.
  • You will understand that dictionary keys must be hashable.
  • You will learn how to read, write, and work with structured data using Python’s JSON module.

Terms and Python keywords introduced

  • del
  • dictionary
  • field name
  • hashable
  • JSON
  • JSONDecodeError
  • key
  • KeyError
  • named tuple
  • set
  • value
  • view objects (keys, values, items)

Copyright © 2023–2025 Clayton Cafiero

No generative AI was used in producing this material. This was written the old-fashioned way.