From the course: Building Recommender Systems with Machine Learning and AI

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Data structures in Python

Data structures in Python

- [Teacher] Next, let's discuss lists in more depth since we use them a lot. If you need to know how many elements are in a list, you can use the built in len function to get that, like so. Often you also need to slice lists in certain ways to extract values in a given range within the list. The colon is used for this. In this first example, we use colon three to ask for the first three elements of the list and similarly, we can use three colon to ask for everything after the third element, like so. We can also do something like negative two colon to ask for the last two elements of the list and if you want to append a list to another list that's what the extend function is for, like this. This will add the lists containing seven and eight to our original list and if you want to append a single value to a list you can use the append function, like so. Another cool thing about Python is that lists can contain just about any type you want. You can even make a list of lists so let's do…

Contents