Python Lists


A list is a data structure that helps store, and if necessary, manipulate an ordered collection of items, such as a list of email addresses associated with a user account. Lists are contained in square brackets [ ].

Lists are variable length and mutable (can change). Lists allow duplicate elements, as well as, indexing and slicing.

A list is a lot like a string. Both strings and lists allow duplicate elements, as well as, indexing and slicing, and both are sequences. A sequence is a positionally ordered collection of items. However, strings are sequences of characters, while lists store sequences of elements of any data type. Let’s contrast sets. Sets can have NO duplicates.

Lists and their contents are mutable, so their elements can be modified, added, or removed. But strings are immutable.

A list like a long box, divided into different slots. Each slot contains a value, and each value can store data which could be another data structure, such as another list, or a string, float, integer or output from another function. Lists can contain any data type, and in any combination. Again, a single list can contain strings, integers, floats, tuples, dictionaries, and other lists.

When working with lists, you use an index to access each of the elements. Recall that an index provides the numbered position of each element in an ordered sequence. The index is zero-based (starts with 0, not 1). If you need a data structure that uses something other than a series of index numbers, consider a dictionary. It uses keys instead of numbers.

Creating a List

There are two main ways to create lists in Python:

  1. Square brackets: [ ]
  2. The list function: list()

To create an empty list, use empty brackets or the list() function, and create two more lists:

empty_list_1 = []
empty_list_2 = list()
list_1 = [42, 'Alice', 'laptop']
list_2 = list([1, 4.3, 'Bob'])

Below is the output.

[]
[]
[42, 'Alice', 'laptop']
[1, 4.3, 'Bob']

Indexing and Slicing

Indexing is zero-based.

list_3 = ['enter', 'winner', 'switch', 'conscious', 'diplomat']
print(list_3[1])    # zero-based - returns winner

Lists Allow Duplicates

Since lists are indexed, they allow for duplicate values. In contrast, sets do not allow for duplicate values. A set is an unordered collection of unique elements. I have a post here on Python Sets.

Modifying a List (List Mutability)

Lists are mutable (changeable) and strings are not. You can add, edit and remove elements in your list and still have the same list. With strings, you need to create a new string or change the entire string by reassigning it.

list_3.append('Bob')
print(list_3)

The output is:

['enter', 'winner', 'switch', 'conscious', 'diplomat', 'Bob']

There is also insert(), remove(), pop() and clear().

Create a List of Tuples from a number of Lists

If you have several lists, you can create a list of tuples from them. The zip() function “pairs” up the elements of a number of lists, tuples or other sequences to create a list of tuples.

More Information

From docs.python.org, here is an article called An Informal Introduction to Python:Lists.

Leave a Reply