3 Using built-in data containers
This chapter covers
- When to choose lists over tuples and vice versa
- Advanced sorting of lists that consist of complex data types
- Using named tuples as a data container model
- Accessing dictionary’s data
- Hashability and its implications for dictionaries and sets
- Set operations and their applications for non-set data
As a general-purpose programming language, Python provides a range of built-in data types for different purposes, including collection types. These collection types of data serve as containers to hold integers, strings, instances of custom classes, and all other kinds of objects. In every project, we deal with multiple objects at the same time, and these scenarios often require data containers to handle these objects. Every modern language has data containers as their core data models, highlighting the importance of data containers as building blocks for any programming project. As you'll see in chapter 14 when we build our task management app, we'll use data containers for a variety of jobs, such as using a list to hold custom instances of the Task class (chapter 8). In this chapter, we’ll discuss the most common built-in data containers, including lists, tuples, dictionaries, and sets. Please note that this chapter isn’t intended to provide an exhaustive review of all the functionalities related to these data models. Instead, we'll be focused on some essential topics that matter most in our projects.