The List

What’s a list

One of the most fundamental objects in python is the list. A list simply holds multiple objects, these can be of any type. A list can be generated in two ways:

# 1) with the [ ]
In [1]: my_list = [1, 2, 'some value', True]

In [2]: my_list
Out[2]: [1, 2, 'some value', True]

# 2) by converting some other object (an iterable) to a list with the list function
In [3]: my_other_list = list((1, 2, 3, 4, 5))  # this uses a tuple, which will be discussed later

In [4]: my_other_list
Out[4]: [1, 2, 3, 4, 5]

# a list can even hold other lists (nesting)
In [5]: my_nested_list = [[1, 2], [3, 4], [5, 6]]

In [6]: my_nested_list
Out[6]: [[1, 2], [3, 4], [5, 6]]

length, indexing, and slicing

You’ll notice in the example above that we mentioned an iterable. While it’s not strictly the definition you can think of an iterable as any sort of object that is a container for multiple objects. The number of objects in an iterable like a list is also known as the length of the list. Unsurprisingly there is a python function to get the length of an iterable like a list called len()

In [7]: my_list
Out[7]: [1, 2, 'some value', True]

In [8]: len(my_list) # using the lists defined above
Out[8]: 4

In [9]: my_other_list
Out[9]: [1, 2, 3, 4, 5]

In [10]: len(my_other_list)
Out[10]: 5

# note for nested lists it will only give the length of the outermost list
In [11]: len(my_nested_list)
Out[11]: 3

Great, I’ve put my very important data into a list, but how on earth can I get it out? Through indexing and slicing. Indexing is getting out a single item in the list, while slicing is getting out a subset of the list. Python is a zero based indexing language. That’s a big phrase to say that the first (or more accurately the zeroth) item is given a position index of 0, so for example my_list:

Item 1 2 ‘some value’ True
Position 0 1 2 3
In [12]: my_list
Out[12]: [1, 2, 'some value', True]

# to index things use brackets []
In [13]: my_list[0]
Out[13]: 1

In [14]: my_list[3]
Out[14]: True

# trying to index something that does not exist e.g. my_list[4] will raise an exception
# to access items from a sublist in a nested list you use chained brackets
In [15]: my_nested_list
Out[15]: [[1, 2], [3, 4], [5, 6]]

In [16]: my_nested_list[0][1] # returns the second item of the first list in my_nested_list
Out[16]: 2

# you can also access things from the back end of the list:
In [17]: my_list[-1] # the last item
Out[17]: True

In [18]: my_list[-2] # the second to last item
Out[18]: 'some value'

# slicing uses brackets and a :
In [19]: my_list[0:2]  # everything from the zeroth item up to, but not including the 2nd item
Out[19]: [1, 2]

In [20]: my_list[1:]  # everything from the first item up to and including the last item
Out[20]: [2, 'some value', True]

In [21]: my_list[:3]  # everything from the first item up to, but not including the 3rd item
Out[21]: [1, 2, 'some value']

list specific operations and functions

Almost every python object has some functions built into them that will act on the instance of the object. These functions are accessed with a ‘.’, e.g. my_var.some_function(). Lists are no different, here we’ll showcase the operators and keywords associated with lists and two most important functions associated with lists, .append() and .extend():

# adding two lists creates a new list with all of the elements joined together
In [22]: list1 = [1,2,3]

In [23]: list2 = [4,5,6]

In [24]: list1 + list2
Out[24]: [1, 2, 3, 4, 5, 6]

# multiplying a list and an integer creates a new list with the previous list repeated n times
In [25]: list1 = ['spam']

In [26]: list1*3
Out[26]: ['spam', 'spam', 'spam']

# the in keyword asks the question is some element in a list and returns a boolean
In [27]: my_list = [1,2,3,[4,5]]

In [28]: 2 in my_list
Out[28]: True

In [29]: 4 in my_list  # note that only the top most layer of the list is searched
Out[29]: False

In [30]: [4,5] in my_list  # it will also search for more complex objects (e.g. other lists)
Out[30]: True

# .append() adds something to a list
In [31]: my_list = []  # create an empty list

In [32]: my_list.append(1)

In [33]: my_list
Out[33]: [1]

In [34]: my_list.append('my string')

In [35]: my_list
Out[35]: [1, 'my string']

In [36]: my_list.append([1,2])

In [37]: my_list
Out[37]: [1, 'my string', [1, 2]]

Note that when you append a list to a list it creates a nested list. If instead you want to append all of the values of an iterable (like a list) to another list then you need to use the .extend() function:

In [38]: my_list = []  # create an empty list

In [39]: my_list.extend([1,2,3,4])

In [40]: my_list
Out[40]: [1, 2, 3, 4]

# note if you try to pass a non-iterable to extend it will raise an exception

The tuple

There is another basic python object, the tuple. A tuple is denoted similarly to a list, but using () instead of [] tuples are generated and sliced exactly the same as lists, but they are immutable. This concept is beyond this lesson, but will be covered in here