Skip to main content

6.3) List Operations


The operators + and * have a similar effect on lists to their effect on strings.

Concatenating lists is done with +

>>> my_list = [1,'two', 3.0]
>>> longer_list = my_list + [4,5,6] # this operation creates a new list
>>> longer_list
[1, 'two', 3.0, 4, 5, 6]

(Notice that this list contains integers, strings and floating numbers.)

The multiplication operator * is very handy for initialising a list:

>>> list_zeros=[0]*5
>>> print list
[0, 0, 0, 0, 0]

This behaviour might seem a little odd if you know fortran or another similar languages. Just remember that lists are not like vectors or arrays. Later in this course you’ll learn how you can do operations on vectors and arrays or matrices using numpy.