Lists and arrays
Lists and arrays can be represented with a table. Imagine a one-dimensional (1D) vector or a matrix, and you have just imagined a list/array.
Lists and arrays can contain data in them. Data can be anything – variables, other lists or arrays (these are called multi-dimensional lists/arrays), or objects of some classes (we will learn about them later).
For example, this is a 1D list/array containing integers:
And this is an example of a two-dimensional (2D) list/array, also containing integers:
In order to create a 2D list, you have to create a list of lists. Creating a list is very simple, just like this:
L1 = list()
L2 = []
L3 = [3,4,1,6,7,5]
L4 = [[2, 9, -5], [-1, 0, 4], [3, 1, 2]]
Here we create four lists: L1
, L2
, L3
and L4
. The first two lists are empty – they have zero elements. The two subsequent lists have some predefined values in them. L3
is a one-dimensional list, same as the one in the...