# Lists¶

The variables that were discussed in the tutorial on Python basics were all assigned single values, such as strings, integers or floating point numbers. Alternatively, it is also possible to work with variables that contain collections of values. One example of a variable type that can hold multiple values is the list. Lists can be created by surrounding all the values that you want to gather by square brackets. The individual values within such lists need to be separated by commas. The statement below creates a list containing the five days of the working week:

In [ ]:
week = [ "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" ]


The elements in the list above are all strings. The elements do not all have to be of the same type, however. It is also allowed to mix strings with integers, for instance. When lists are created in this manner, Python numbers the elements in the array automatically, following the order in which these values given. These numbers will function as indices that can be used to access the individual items in the array. Bear in mind that Python starts counting at 0. In the example above, the element “Monday” is assigned the index “0”, and the element “Thursday” will have the index “3”. As is the case for strings and their individual characters, the separate elements in the list can be accessed using the bracket operator. Another similarity between lists and strings is that the lengths of both types of variables can be found using the len() function. In the case of lists, len() returns the number of elements in the list.

In [ ]:
week = [ "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" ]

print( week[3] )
# This prints "Thursday"

print( week[-1] )
# This prints "Friday"

print( len(week) )
# This prints 5



The append() method can be used to add items to an existing list. Note that methods, in contrast to functions, need to be pasted onto variables using the period. See listing 2.1. below for an illustration.

In [ ]:
week.append("Saturday")
week.append("Sunday")

print( "The list now contains {} items.".format( len(week)))
# This prints the following sentence:
# 'The list now contains 7 items.'


Finally, the items in a list can be arranged alphabetically or numerically by making use of the sorted() function. The code below offers an illustration.

In [ ]:
unsortedList = [ 5 , 8 , 2 , 9 , 1 , 8 ]
print( sorted( unsortedList ) )

## The output is as follows:
## [1, 2, 5, 8, 8, 9]


To navigate across all the elements in a list, you can make use of the ‘for’ keyword. As can be seen in the code below, ‘for’ needs to be used in combination with ‘in’ for this purpose. After 'in', you need to mention the name of the list whose elements you want to retrieve, and after 'for', you need to supply the name of a new variable. In the example below, this new variable is named ‘day’. During each cycle, the value of the variable ‘day’ will be different; it will consecutively be assigned the various elements in the list named ‘week’. The loop will continue as long as there are items in the list.

In [ ]:
for day in week:
print( day )