From the course: Programming Foundations: Beyond the Fundamentals

Understanding collections - Python Tutorial

From the course: Programming Foundations: Beyond the Fundamentals

Understanding collections

- Despite my best efforts at digital organization, sometimes I have projects that involve working with sheets of paper, lots of sheets of paper. If I have enough room, I can just spread them all out and then grab the one I need for each tack that I'm doing. But when the paper multiplies, I don't have enough room for it all, so I make a pile. And that makes sense when my papers are related, especially, if they're papers that I'll need to use together. A pile keeps related papers together and keeps them organized. It also makes sure that when I know the type of information I need, but not the specific sheet it's on, I can easily grab all of them. If my papers start multiplying, it can make sense to organize them into groups. I can grab some file folders, add some labels. I can even hang them up for easy access. Even though I have more papers, the labels make it easy for me to find specific papers when I need them. Grouping similar items together is also useful in programming, and it's so common. It has a name, a collection. Like a pile of papers, a collection in programming lets me group similar items together using a single name, which is known as a variable. Organizing data in this way has a few advantages. First, creating a collection uses your code structure to indicate that multiple pieces of data are related. So rather than storing a bunch of values and needing to identify which ones are relevant for different processes in my program, all the related data is grouped in a single place. Second, using a collection avoids creating a potentially huge number of variables to track within my code. Instead, all the related data is stored under the same variable name with syntax to retrieve or change the specific data I need at any point. And finally, it's common to need to access multiple related pieces of data and then perform the same process on each piece of data. A collection offers simplified syntax for performing this type of process. Collections can take different forms and programming languages vary in the types of collections they support. Learning how to create and use collections in programming will help you to write more efficient and better organized code that can easily work with data from a wide variety of sources.

Contents