Python Basics - Lists

Python has a built-in list type. The list literal is created using square bracket syntax '[]'. All the items (elements) inside these square brackets is what comprises the list.
Below is an example of of an alphabetical list holding three elements. Each element can be selected using the slice syntax [], and the length of the list can be shown using the len() function:

When assigning a list to a new variable with the '=' operand, it is important to note that the list is not 'copied', but rather the same list is assigned to both its original variable and the new one:

Values can be used from a list for many different purposes. A simple one is to take an individual value from a list, for example the first element, and incorporate it into an f-string:

Perhaps a value is not correct, or needs to be changed. It is possible to change any value in a list, for example swimming is changed to football below:

Sorting

Lists can be sorted in many different ways. An obvious and useful choice is alphabetical sorting, Python has a sort() method that does just that:

The sort() method changes the order of a list permanently. If a list needs to be temporarily sorted, the sorted() function is the better option.

FOR and IN

In Python a 'FOR' loop is used to iterate over a sequence. This sequence can be a list, tuple, dictionary, set or string. The FOR loop is an extremely useful way of looking at each element in a list. The next example iterates through the list called 'counting' and prints each element within:

Next, instead of printing each element, the FOR loop iterates though the counting list. Each time an element is reached, the value is added to the 'sum' variable. After the FOR loop has completed, the sum of all the numbers in the counting list is printed:

By using the IN construct it is also possible to check if an element is in fact present. The next example checks if a specific string is present in the sports list, depending on how the if statement evaluates, a confirmation message is printed:

A simplier way to achieve the same evaluation as above is to use 'in' with the print function. If the string is present in the list, the output is true:

Range

In Python the range() function returns a sequence of defined numbers. This range starts at 0 (n-1). By using range() in a FOR loop, it is possible to build a numeric FOR loop:

While Loop

Python has a while loop built-in. This is very useful when needing to excute a set of statements while a condiction is true. The example below prints the value of the variable 'i' while the value is less than 5:

It is possible to break out of the while loop when a certain condition is met. For example, once the value of 'i' is equal to 3:

Conversely, it is also possible to continue when a condition is met. Here, the current iteration is stopped, and the program continues with the next:

Append

Appending to a list in Python is straightforward. It is important to note that when appending to a list, the original list is modified.

To append an element at a specific location in the list, the insert command works, which requires the position followed by the element to insert:

A pattern used often to build up a list is to start with an empty list and append elements to it:

Basic Statistics

Some basic statistics can be run on lists. Below a range of numbers is created, and the min() and max() values are reported:

Summary

This post explains what lists are and how to use them. Adding to a list, changing the order of a list, as well as various methods to manipulate lists was shown. A brief introduction to the concept of iteration using 'IN' and 'FOR' was also discussed.