Chapter 9 Lists

9.7 Initializing a List

There are four common ways to initialize a list. The first is to make an empty list (i.e. [ ]); the second is to make a list of length N, consisting of the same constant x, for which the expression [x] ∗ N can be used; the third is to make a list that contains a number sequence, for which the range function can be used; the fourth is to make a copy of a given sequence, for which a copying operation can be used. If the list to be initialized is more complex than the cases above, it should be initialized using a specific loop.

A list of empty lists can be initialized using a simple loop.

When initializing a list by making a copy of a sequence object, the given sequence object can be a list, a tuple or other sequence types. In the case of a list, the list copying techniques introduced before can be used. In the case of a tuple, the list function converts a tuple into a list by making a shallow copy of the tuple.

© Copyright 2024 GS Ng.

Next Section - 9.8 Lists of Lists