Loading video player…

Records and Sets: Selecting the Ideal Data Structure (Overview)

There are a variety of ways for storing and managing data in your program and the choice of the right data structure has an effect on the readability of your code, ease of writing, and performance. Python has a wide selection of built-in mechanisms that meet most of your data structure needs. This course introduces you to two types of data structures: data records and sets.

There are multiple types and classes for both of these data structures and this course discusses them and provides information on how to choose the right one.

In this course you’ll learn about:

  • What are the advantages of using the built-in dict type as a Data Record
  • What flexibility do DataClasses add for Records compared to regular Classes
  • How to store C-type Data with struct.Struct
  • How to define Sets
  • Where to use a Frozenset
  • How to create a counter with a multiset

This course is the second part of an ongoing series, exploring how to find the right Data Structure for your projects. The first course is Dictionaries and Arrays: Selecting the Ideal Data Structure and the third part is Stacks and Queues: Selecting the Ideal Data Structure.


Sample Code (.zip)

3.1 KB

Course Slides (.pdf)

633.9 KB

00:00 Welcome to Selecting the Ideal Data Structure: Data Records and Sets. My name is Chris and I will be your guide. In this course, you’ll learn about common abstract data types that are used in Python—specifically, records and structs, and sets.

00:17 A quick note: All code samples here were tested using Python 3.9. Most of the concepts have been around for quite some time, so if you’re using an earlier version, you shouldn’t run into any trouble and I’ll do my best to point out any version differences as I go along.

00:34 An important part of designing and coding your program is figuring out how to store your data and how to organize it. Different kinds of data structures have different strengths and weaknesses and purposes, and choosing the right one will affect how easy it is for you to code as well as how performant your code will be.

00:52 Python likes to think of itself as a batteries included language, and as such, comes with most of the common data structures that you could possibly need.

01:00 This course focuses on two of those types of data structures. The first is data records and the second is sets. A previous course on data structures covered dictionaries and arrays.

01:12 A link to that will be in the notes below in case you wanted to check it out.

01:17 This course is divided off into two sections. The first section is records and structs, and is comprised of multiple lessons. What is a record or struct?

01:27 It’s a way of grouping related fields into the same data structure. A common simple way of doing this is using the built-in dict type, a dictionary, in order to do this kind of grouping. If you want or need more control than that, you can start using some of the object-oriented features of Python, but that often means an awful lot of extra code.

01:48 The data class concept provides a shortcut for writing classes, requiring less boilerplate than if you were just doing it normally. And the last record that I’ll talk about is from the struct library.

02:00 The Struct object is a way of grouping C type fields together. This can be useful if you are working with network data, binary data, or interacting with a Python extension that uses the C language.

02:14 Once I’ve covered all of these concepts, there’ll be an additional lesson on how to choose between these different implementations and where to get more information.

02:23 The second section is on sets. A set is an unordered collection of unique items. Python provides two built-in types to do this kind of work. The first is called set and the second is called frozenset.

02:37 These are the mutable and immutable versions, respectively.

02:42 A close cousin to sets is sometimes called a bag. In the collections library, the Counter object performs this need. Like a set, it’s a collection of unique items. Unlike a set, it tracks how many times you put in each unique type over and over again, keeping a counter—hence the name of the object.

03:04 Next up, I’ll start the first section and dive into records.

Become a Member to join the conversation.