Home Lists in Python
Post
Cancel

Lists in Python

Lists are an important tool used in Python. Lists can contain elements of mixed types as well. Let’s look at a few examples.

1
2
3
# List
list1 = []
list1
1
## []
1
2
list2 = [1, 'UBC', 100]
list2
1
## [1, 'UBC', 100]

Now let’s see a few operations we can perform on a list.

1
len(list1)
1
## 0
1
len(list2)
1
## 3
1
2
# Indexing Example - we will talk more about this later on
list2[2]
1
## 100

Note - Python indexing starts at zero, so the first element in the list is 0, the second is 1, and the third is 2, so if we actually wanted the second element, we would have to do this:

1
list2[1]
1
## 'UBC'
1
type(list2)
1
## <class 'list'>
1
type(list2[1])
1
## <class 'str'>

As we mentioned earlier, a main difference between lists and tuples is that lists are mutable. Mutable means that elements in a list can be appended, changed, or deleted.

1
2
3
mixed_list = [1, 'UBC', 5.0, '1']
mixed_list.append('New')
mixed_list
1
## [1, 'UBC', 5.0, '1', 'New']

If we decided that we wanted to replace ‘New’ with 4, we could do so like this:

1
2
mixed_list[4]= 4
mixed_list
1
## [1, 'UBC', 5.0, '1', 4]

If we wanted to remove elements from the list, we could do so like this:

1
2
mixed_list.remove('UBC')
mixed_list
1
## [1, 5.0, '1', 4]

Note that this will only remove the first occurrence, if this happened to be in the list more than once. It would also call an error message if the element doesn’t exist.

If we wanted to remove it based on the index of the list:

1
2
del mixed_list[0]
mixed_list
1
## [5.0, '1', 4]
This post is licensed under CC BY 4.0 by the author.