Skip to content Skip to navigation


You are here: Home » Content » Umonya: Introduction to programming: Lists


Recently Viewed

This feature requires Javascript to be enabled.

Umonya: Introduction to programming: Lists

Module by: Johan Beyers. E-mail the author

Summary: List concepts and basic list operations.

Why lists?

Why do we need lists?

In the real world, you often need to do the same thing with many objects. For example, you would like to have a list of people in the class. You could use one variable per person, but then you have no way to do something with each person. For this, we use a list.

A list can store multiple items. Each item is called an element.

a = [10, 20, 30]

This list has 3 elements.

a = ['Bob', 'Jane', 'John', 'Pete']

Lists elements can be any type of variable.

a = [15, 'Pete', 3.14]

You can even store different types of elements in the same list.

a = [10]

This list has only one element.

a = [['Bob', 'Smith'], ['John', 'Ellison']]

A list can even contain other lists.

a = []

A list can be empty.

We can see how many elements are in a list by using the len function:

>>>the_class = ['John', 'Jane', 'Pete']

The len function gives you the length of the list.

The elements in a list stay in the order they were added. Each element has an index number associated with it. The numbers start at 0.

>>> team = {'Alice', 'Bob', 'Charlie', 'Daniel']
>>> team[0]

The first element in the list has index 0.

>>> team[1]
>>> team[2]
>>> team[3]

Each item can be accessed by using its index.

>>> team[2] = 'Cecil'
>>> team
['Alice', 'Bob', 'Cecil', 'Daniel']

We can change any of the elements in a list, similar to the way we change variables.

>>> team.append('Elaine')
>>> team
['Alice', 'Bob', 'Cecil', 'Daniel', 'Elaine']

We can append elements to the end of the list.

To get only some of the elements of a list, we can slice the list. To do this, we have to specify where in the list we want to start and end. The start index is the first element in the list. The end index is the first element that will NOT be included in the slice.

>>> team[0:1]
>>> team[0:2]
['Alice', 'Bob']

Slicing returns the elements from the index of the beginning of the slice to the element just before the end index.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens


A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks