Python Lessons – 2.7 Lists

Python Lessons - 2.7 Lists

Lists

In the Python language, lists are a type of object. This particular type of object is used to store a series of values in an indexed sequence.

A list is defined when using a sequence of values enclosed within square brackets,

and to access its values it is sufficient to write the name (like all the other variables).

In case you want to access only one of the values of the list, for example the third one, you will write the name and the corresponding index which is 2 (and not 3 !!!! the indexes in Python start from 0).

It is possible to create an empty list and then fill it afterwards during the program execution.

A list can also contain values of different types, including other lists.

List of lists

In Python there are no multidimensional arrays (matrices) and therefore lists are used to represent them.

In this case, to access an element of an internal list, two indices must be called, the first referred to the general list, the second referred to the internal list.

IndexError

A very common mistake is the request for an index beyond those defined. In this case we have an IndexError.

Strings as lists

Another curiosity is that so far we have used strings. Well these strings can be considered lists of characters

⇐ Go to Python Lesson 2.6 – WHILE loop

Go to Python Lesson 2.8 – Operations on list ⇒

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.