Intro to Lists

In this section we are going to learn about the list data type in Python. Python lists are examples of sequence types because they behave like a sequence (an ordered collection of objects).

Here we define two lists so we can learn by example. Be sure to create a Jupyter Notebook and call it intro_to_lists and follow along.

fruits = ['apple', 'orange', 'banana']
numbers = [1,2,3,4]
type(fruits)
list
type(numbers)
list

A list in Python is just a list of objects separated by commas and enclosed between two square brackets []. The objects within the list can be of any data type. Here is another list with strings, integers, floats, and boolean data types mixed together.

another_list = ['hello world', 3.1459, -10, 0, 'hey', 1 + 5, True, False]
print(type(another_list))
print(another_list)
<class 'list'>
['hello world', 3.1459, -10, 0, 'hey', 6, True, False]

Here is an empty list.

[]
[]

Here is a list with duplicates, which is totally fine.

[1,2,1,1,2,2,2,2,'chris','hello',True,False,False,True,True,2,2,2]
[1,
 2,
 1,
 1,
 2,
 2,
 2,
 2,
 'chris',
 'hello',
 True,
 False,
 False,
 True,
 True,
 2,
 2,
 2]

List Indexing and Slicing

We can index and slice lists the same way we did with strings. Here are examples. Note that the syntax is the exact same.

print(fruits)
['apple', 'orange', 'banana']
fruits[0]
'apple'
fruits[1]
'orange'
fruits[2]
'banana'
fruits[-1]
'banana'
fruits[-2]
'orange'
fruits[0:2]
['apple', 'orange']
fruits[0:10]
['apple', 'orange', 'banana']
fruits[10] # error because outside the index range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-6aac45e6b062> in <module>
----> 1 fruits[10] # error because outside the index range

IndexError: list index out of range
print(another_list)
another_list[::2] # from start to end and taking every 2nd element
['hello world', -10, 'hey', True]
another_list[::-1] # reversed list
[False, True, 6, 'hey', 0, -10, 3.1459, 'hello world']

len() function

You can use the len function to get the number of items in an object. The types of objects we have seen for which we can use len() are strings and lists.

# getting the length of a list
print(numbers)
print(len(numbers)) # there are 4 items in the list
[1, 2, 3, 4]
4
print(another_list)
print(len(another_list)) # there are 8 items in the list
['hello world', 3.1459, -10, 0, 'hey', 6, True, False]
8
# getting the length of a string
x = 'Hello World !'
print(len(x)) # there are 13 characters in the string
13

append() to List

The list object is an example of a mutable object. This means you can change it after it’s created. For example, you can append items to a list, remove items from a list, and so on. Simply put, a mutable object can be changed after it is created, and an immutable object can not. Examples of objects we have seen so far which are immutable are integer, string, and float objects. Lists however are mutable.

Let’s define a list and then add items to it with the append() method. A method is sort of like a function but different. Do not worry about it for now. Just know there exists a method called append and here you will see how to use it.

my_list = ['sunny day',-5]
print(len(my_list))
print(my_list)
2
['sunny day', -5]
my_list.append(2.71)
print(len(my_list))
print(my_list)
3
['sunny day', -5, 2.71]
my_list.append('bye')
my_list 
['sunny day', -5, 2.71, 'bye']

Notice that the append() method modifies the list in place. There is no need to assign the result to another new variable name.

List Concatenation with +

Just like we concatenated strings with the + operator, we can also do a similar thing with lists.

a = [1,2,3]
b = [4,5,6]
c = a + b
print(a)
print(b)
print(c)
[1, 2, 3]
[4, 5, 6]
[1, 2, 3, 4, 5, 6]
c = a + a + a + a
print(c)
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

extend() method

The extend() method adds all the elements of a list to the end of the list. It modifies the list in place. Let’s look at an example.

# we already defined these two lists
print(f'a is {a}')
print(f'b is {b}')
a is [1, 2, 3]
b is [4, 5, 6]
a.extend(b)
print(b) # b stays the same
print(a) # the items from b were added to a
[4, 5, 6]
[1, 2, 3, 4, 5, 6]

Lists are Mutable

We just saw that lists are mutable which can lead to some “confusing” behavior for beginners in Python. We will learn more about this sort of thing as the course progresses. Here is another example of editing/changing a list after it’s created. It will change any variable that is a reference to that object.

list1 = [1,2,3]
list2 = list1
print(list1)
print(list2)

list2.append(99) # changes the object in place. Does not create a new object.
print(list2)

print(list1) # list1 and list2 both are references to the same object and are both mutable
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 99]
[1, 2, 3, 99]
# here is another example using .extend
list1 = [1,2,3]
list2 = list1
print(list1)
print(list2)

list2.extend([4,5,6]) # changes the object in place. Does not create a new object.
print(list2)

print(list1) # list1 and list2 both are references to the same object and are both mutable
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
# here is another example using  + 
list1 = [1,2,3]
list2 = list1
print(list1)
print(list2)

list2 = list1 + [4,5,6] # this + operator creates a NEW object
print(list2)

print(list1) # list1 and list2 are references to different objects
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
[1, 2, 3]

Membership Operators (in, not in)

We can use the operators in and not in to check if something is in a list or even in a string. It returns a boolean value True or False.

print(my_list)
['sunny day', -5, 2.71, 'bye']
-2 in my_list # the object -2 is not one of the items in my_list
False
-2 not in my_list # the object -2 is not one of the items in my_list
True
4 in my_list # the object 4 is not one of the items in my_list
False
4 not in my_list # the object 4 is not one of the items in my_list
True
'sunny day' in my_list # the object 'sunny day' is one of the items in my_list
True
'sunny day' not in my_list # the object 'sunny day' is one of the items in my_list
False
2.71 in my_list # the object 2.71 is one of the items in my_list
True
2.71 not in my_list # the object 2.71 is one of the items in my_list
False
name = 'chris'
if name in my_list:
    print(f'{name} is IN the list {my_list}')
else:
    print(f'{name} is NOT IN the list {my_list}')
    
chris is NOT IN the list ['sunny day', -5, 2.71, 'bye']

You can also use the in and not in operators with strings.

s = 'chris is nice'
print(s)
chris is nice
'chris' in s
True
'Chris' in s # because of the capital C
False
'is  ' not in s # because two spaces 
True
'is is ni' in s
True

Change Item Value in List

We just saw how lists objects are mutable. This means you can change them or edit them after they are created. You can change the item in a list using indexing and assignment with the = operator.

print(my_list)
['sunny day', -5, 2.71, 'bye']
print(my_list[1])
-5
my_list[1] = 5 # we are changing the item at index 1 to 5.
print(my_list)
['sunny day', 5, 2.71, 'bye']
my_list[-1] = 'chris'
my_list
['sunny day', 5, 2.71, 'chris']
my_list.append(5*6)
my_list
['sunny day', 5, 2.71, 'chris', 30]
new_list = [1,2,3]
my_list.append(new_list) # you can append any object to the list my_list, even another list new_list!
my_list
['sunny day', 5, 2.71, 'chris', 30, [1, 2, 3]]
my_list[-1]
[1, 2, 3]
my_list[-1].append(4)
my_list
['sunny day', 5, 2.71, 'chris', 30, [1, 2, 3, 4]]
new_list # oh wow it changed! We will talk more about this later.
[1, 2, 3, 4]
[1, 2, 3, 4] in my_list
True

There is a lot more to learn about lists but we will do so later.