Multi-Dimensional Lists
Multi-Dimensional Lists
Multi-dimensional Lists
Up to now, we only looked at one-dimensional lists where a list holds values within
square brackets. Python allows the creation of multi-dimensional lists as well. Consider
the following matrix presented in Figure 2.
If you are unfamiliar with the concept of a matrix, consider it as a simple structure where
values are stored in multiple rows and columns. A value in a matrix is denoted by amn
where m is the row number and n is the column number (see Figure 2).
8. List Operations
This section discusses some other common list operations.
Length
To find the size/length of a list, the function ‘len’ (stands for length) can be used. See
the following example.
>>> len([1,2,3])
3
The length of this list is 3 as the list contains 3 values.
Concatenation
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> print(a+b)
[1, 2, 3, 4, 5, 6]
Suppose there are two lists a and b. The contents of lists a and b can be combined
using the plus(+) operator. a+b will output a single list with contents from lists a and b.
In the output of the above example, values 1, 2, and 3 are from list a, and values 4, 5,
and 6 are from list b.
Repetition
The following code illustrates how repetition works.
>>> print([‘Hi’] * 4)
[‘Hi’, ‘Hi’, ‘Hi’, ‘Hi’]
Note that in the above example, multiplying by 4 does not create 4 separate lists but a
single list where the contents of the original list are multiplied 4 times.
Membership
Membership checks whether a value is available in a list. See the following example.
>>> print(3 in [1,2,3])
True
The ‘in’ operator is used to check membership. Statement ‘3 in [1,2,3]’ checks whether
value 3 is available in the list. Since the value is available, it returns True. If the value is
not available, it will return False.
Iteration
Iteration means going through the list one element at a time.
>>> for x in [1,2,3]:
print(x)
1
2
3
The first element in the list, which is 1 in the example above, will be assigned to the
variable x. Then the print(x) statement will be executed. After that, the second value in
the list, which is 2, will be assigned to the variable x. The print(x) statement will be
executed again. This pattern continues until the last element in the list. It might not be
very clear at this point. The concept of iteration will be discussed in detail in the next
lesson.
Negative Indices
Figure 4 illustrates how the list L is indexed using normal indexing and negative
indexing.
In negative indexing, the last value of the list is indexed -1, similar to how the first value
is indexed 0 in normal indexing. The rest of the elements are indexed from right to left.
In the given example, the last element ‘c’ is indexed -1, the next value to the left ‘b’ is
indexed -2, and ‘a’ is indexed -3. Therefore, ‘print(L[-2])’ will output ‘b’.
Slicing
Slicing means extracting a part of the list. We have learned about this earlier as well (in
Section 3), but without using the term slicing. Using the range m to n within square
brackets (L[m:n]) will consider the values from index m to index (n-1). In addition to what
has been discussed in previous sections, the second part of the range is empty in L[1:].
It means from index 1 up to the last value of the list. Hence, L[1:] outputs the values ‘b’
and ‘c’.