Lists

Lists are part of the core Python language and probably the most used data structure. Despite their name, lists are implemented as dynamic arrays. This means that elements can be added or removed dynamically and the list will automatically allocate or release memory.

They can be defined with comma-separated values between square brackets.

>>> l = [1, 1, 2, 5, 14, 42, 132]
# Lists have a nice representation by default
>>> print(l)
[1, 1, 2, 5, 14, 42, 132]

We can also contain a list inside another list which allows us to easily describe

In Python, lists are heterogeneous: We can store different kinds of data types in a single list.

>>> l = [0, True, 'two', 3.14]

The drawback of the structure being so supple is it that takes up more space since data is less tightly packed and you can no longer expect what to find inside.

Getting information

Indexing and slicing

As with strings, lists can be indexed and sliced:

>>> l = [1, 6, 15, 28, 45, 66, 91]
>>> l[-1] # Elements at the end can be accessed with negative numbers, starting from -1
91
>>> l[:2]
[1, 6]
>>> l[::-1] # With a negative step, the list is reversed
[91, 66, 45, 28, 15, 6, 1]

It is also possible to unpack a list. For instance,

>>> l = ['a', 'b', 'c']
>>> a, b, c, = l
>>> print(a, b, c)
a b c
>>> a, b = l
ValueError: 'too many values to unpack (expected 2)'

Here we combine these two capabilities:

>>> l = ['a', 'b', ['x', 'y', 'z']]
>>> x, y = l[2][:2]
>>> print(x, y)
x y

Modifying

Unlike strings, Python’s list objects are mutable so it is possible to change their content.

>>> l = [1, 6, 15, 28, 45, 66, 92]
>>> l[-1] = 91
>>> l
[1, 6, 15, 28, 45, 66, 91]

Moreover, we can make assignments to slices and even change the length of the list while doing so:

>>> l = [1, 6, 15, 28, 45, 66, 91]
>>> l[2:5] = ['a', 'b']
>>> l
[1, 6, 'a', 'b', 66, 91]

These are some of the most useful methods available for lists.

Method Description
append(item) Adds an item to the end of the list.
extend(other_list) Extends the list by appending all the items from the iterable.
insert(index, item) Inserts an item at a given position.
remove(item) Removes the first item from the list whose value is equal to item.
pop() Removes and returns the last item in the list.
del statement Removes an item from a list given its index instead of its value. Its syntax is slightly different, del list[index].
sort() Sorts the items of the list in place.

For example,

>>> l = [1, 6, 15, 28, 45, 66]
>>> l.append(91)
>>> l2 = [231, 190, 153, 120]
>>> l.extend(l2)
>>> l
[1, 6, 15, 28, 45, 66, 91, 231, 190, 153, 120]
>>> del l[4]
>>> l.remove(28)
>>> l.pop()
>>> l
[1, 6, 15, 66, 91, 231, 190, 153]
>>> l.sort()
[1, 6, 15, 66, 91, 153, 190, 231]

Creating new lists

We will now take a look at some operators and functions which return new lists instead of modifying them in place.







Lliçons.jutge.org
Víctor Adell
Universitat Politècnica de Catalunya, 2019

Prohibit copiar. Tots els drets reservats.
No copy allowed. All rights reserved.