From the course: Programming Foundations: Data Structures
Create a list in Python - Python Tutorial
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
-
-
-
-
What is an array?2m 53s
-
Create a list in Python1m 48s
-
(Locked)
Retrieve data from a list in Python3m 39s
-
(Locked)
Mutate a list in Python2m 45s
-
(Locked)
Solution: Swap items2m 34s
-
(Locked)
Multidimensional lists in Python4m 36s
-
(Locked)
Tuples in Python2m 17s
-
(Locked)
Solution: Square items1m 52s
-
(Locked)
Search array-like structures3m 47s
-
(Locked)
Sort array-like structures3m 27s
-
(Locked)
Solution: Find the second smallest item2m 2s
-
(Locked)
When to use array-like structures1m 38s
-
-
-
-
-
-
-
-