Back

Lists

Introduction
A list is a container, which holds a collection of items in an order. The list can be empty, or contain similar items (called 'elements'), like a list of numbers, or a mixture of items, such as integers, floats, strings, booleans and a list can contain other containers, such as other lists. The first item (called an 'element') is at position zero.To create a list, we use square brackets and assign a name to it.

For example:

letters = ['a', 'b', 'c']
nums = [10, 20, 30, 40, 50]
bigList = [34.5, True, letters, nums]
empty = []

for item in bigList:
    print (item)

Once we have a list, we can do all kinds of things to it. We can:

add items to it
remove items from it
combine lists,
reorder them into different orders
get any individual element from the list
get a subset of elements from the list, called splicing
find the position of a particular element
search through the list
count the elelments in a list
print the lelements out
find the biggest and smallest values in a list
convert the list container into different types of container

In our Python course on this website, you can go through the many different exercises on lists. Using them is the best way to understanding them.  

Back