7Lists

In this chapter we will cover lists, not the kind you write down your food shop but a really important part of Python. Lists are the first of the storage types we consider from core Python, the others being Tuples, Dictionaries, and Sets. Fundamentally lists allow us to store things, let's say we want to have a variable containing the numbers 1–10, we can store them in a list as follows:

image

What we have here is a list of integers, we could do the same with a list of strings.

image

The beauty with lists is that we could have a mixture of things in it, so it could look like this:

image

We could even put variables in a list, so if we take the previous example we could have replaced some of the entries with the variable equivalent

image

We can even put lists in lists, like so

image

Basically lists are really powerful, however given you have put something in it you then need to be able access it. Lists in Python are 0 indexed which means to access the first element of the list you need to do the ...

Get The Python Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.