We will now illustrate some of the benefits of lists for programers. As they show, the main benefit is the ability to store more than one value, which can help in some cases. Furthermore, their dynamic nature allows programmer to add items to lists when necessary and solve problems which could not be solved with single variables alone. Also, they can be used within a for-loop to automate programs.
First, lists can be used to manage several cases where a similar computation is done but for different cases. In these cases, lists and for-loops help us in copying and pasting the code for several cases. Code Example 5.4 shows how a list is used to store constant values that are then used in the code later on. This is similar to cases where we read variables from a file, but here we can keep it tighter and easier to maintain. (Lists allow storing data other than numbers in them, like the value `Catâ. They can also store data other than text and numbers, as in other lists. We will discuss this later.) Thus, lists serve as a static storage. They are present with certain values and are used together with a for-loop to avoid copying and pasting code. Lists can also help define conditions, such as when checking for multiple persons of interest in network analysis (see Code Example 5.5): observe how in is used within an if-statement.
Alternatively, lists can be used to collect data when their size is unknown. The first case is when we use lists to store variables of interest for further analysis. This can help to divide the problem into a smaller, less complex problem. First, consider the problem of selecting correct cases, and second, conduct the further processing depending on a task. For example, if our interest is only on messages with specific criteria (see Code Example 3.1), we could focus on each of the stages separately instead of doing highly complicated and nested code.
Furthermore, there are cases where the lists really need to dynamically grow as further cases are observed. For example, if we examine networks and seek to identify if there is a path between nodes A and X, we must determine if each node A is connected, analyse with what nodes they are connected and so on. (See Section 4.4.1 for details and visualisation.) We need to store two details for this algorithm: the nodes we have already examined (coloured in the network graph) and the nodes we still need to visit. We do now know these numbers before they are available for us. Therefore, we must use a dynamic structure to store both details, as shown in Code Example 5.6. Similar problems may emerge in algorithmic data analysis, data pre-processing or in any computational task. For example, if we seek to store all unique words in a file, we do not know how many words there are. Thus, we need to store the words in a list, as shown in Code Example 5.7.
Finally, the lists are indexed. This means we know the first value entered into a list, the second and so on. In Figure 5.2, the first entered value is 25, the second entered value is 55 and the last entered value is 65. This information can benefit programmers in some cases. For example, if we are interested in the median value of numbers in a list, we first sort the list and choose the value that is half of the total length. (Similarly, we could use this to find the highest value or third-lowest value.) Sometimes data make sense only in the order where it has been given. For example, the names of Roman emperors can be listed in descending order. This order itself has meaning that cannot be captured by variables alone.