From the course: Programming Foundations: Data Structures

Create a list in Python

- [Instructor] We've talked about arrays in theory, but now it's time to put them to practice in Python with a list. Imagine we want to compute the average number of books each student in a class has read this year. This is a good use case for a data structure because we need a way to store and access each student's individual number of books as well as compute the average. First, let's create a list to represent the number of books each student has read. Each number in the list corresponds to the number of books read by a specific student. Here we're initializing the list with values that represent this data. While it's technically possible to mix different types of data in the same list, it's generally best to stick to similar types to avoid confusion and save memory. Lists, like other collections, are typically used to store related data. Now back to our example, to compute the average number of books read, we first need to determine how many students are in the class. This means we'll need to calculate the length of the list. We can do this easily using python's built-in len function. This'll tell us how many numbers, or in our case, students are in the list. Let's print it out to the console. Let's run it. There are 16 students in the class. To calculate the average, we'll need both the total number of students and the sum of all the books read by them. In order to sum the contents, we'll need to learn how to access the data in our data structure, and we'll do that in the next video.

Contents