Skip to content

Introduction to Python – Chapter 8 – Lists, Matrices, and Tuples

In the last video, we talked about lists.

There are a couple of more things that we need to talk about lists including matrices.

Let’s say that we had two lists:

With lists, a and b are the same thing. If I change b, then a is also changed.

Let’s test changing b.

When we run the program, we can see that even though a was a list with 1, 2, 3, 4, and 5, and we didn’t change a, a’s value at index 0 is now 0.

We assigned b at index 0 to 0.

What we find out is that changing b also changes a.

With lists, b = a is an assignment by reference.

Assignment by reference is a fancy way of saying that there’s an assignment pointing b to a.

b = a is not a copy of a. b and a are the same variable.

The variable b points to where a was created, so changes to b affect a.

Instead of doing this example with lists, if I were to use integers or strings, assigning b to a would be an assignment by value.

Assignment by value means that there is a copy of the variable, which makes b independent to a.

Remember that assigning b to a with lists is an assignment by reference. b and a are the same variable.

This aspect of lists affects how we use lists in functions.

Let’s make a function to delete the end of a list.

This function will delete the last value of a list.

When you use a list as an argument in a function, the list is passed by reference.

Passed by reference means that whatever we do to the variable will change the original.

Even though the parameter has a different variable name, the list_parameter and list_argument are in fact the same variable.

list_parameter is not a copy of list_argument. There is only 1 variable. They are the same variable.

Any change that happens to list_parameter affects list_argument.

We can add a print(list_argument) to see if the deletion of the last value in the list_parameter affects list_argument.

When we run the program, we will see that list_argument has 1, 2, 3, and 4. 5, which was the last value was deleted.

If we were using integer or string parameters with functions, those arguments are passed by value.

Passed by value means that there is a copy of the variable. The variables will be independent.

Remember that using a list as an argument will be a pass by reference. The argument and parameter will be the same variable.

We didn’t talk too much about using a list in a list.

A list in a list is used to create a matrix.

A matrix is a fancy way of saying a table of data.

Just think about a Microsoft Excel spreadsheet.

If we think about a list, it’s just a row of comma separated values.

The difference between a matrix and a list is that a matrix has multiple rows and columns.

A 3 by 3 matrix would be a 3 by 3 table with data:

Each row is a separate list, meaning that each row is enclosed in brackets.

Then, the entire matrix is contained in a list.

We can write this matrix in a single line like:

The matrix is a list. Then, each row is a list element in the list.

How we access a value in a matrix is we use the index from the row, followed by the index of the column.

For example, if I wanted to get the first value in the matrix, which would be row, 0, and column, 0, I would write.

You can extract any matrix value this way. Just think of using the double brackets, get the row first, then the column second.

When we run the program, we get 1, because 1 is at row, 0, column, 0, of the matrix.

You can use for loops with matrices.

When we run the program, we’ll get all the values of the matrix.

In this for loop, the first for loop traverses the rows.

Then, the second for loop looks at the length of the current row in order to traverse the columns.

Inside the column for loop, we’re accessing the cell at the row column index.

The last thing that we’ll talk about lists is that strings can be converted into lists, and lists can be converted into strings.

To convert a string into a list, you can use the .split() function.

The .split function works well with comma separated values.

The csv files that you’d normally use with Microsoft Excel or a different spreadsheet program.

Let’s say that we had a string:

We can see that there are words in the string separated by commas.

This string would be much more useful to us as a list.

Inside the parentheses, you put the character or string that you want to separate the string by.

I want to separate the string by its commas. Every time, there is a comma, it creates a new value in the list.

When we run the program, we can see that fruit_list is a list where each value is the fruit string before a comma appeared.

We have apples, bananas, mangos, oranges, and strawberries each as a value in the list.

Next, we can change a list to a string.

Let’s say that we had a list that looked like a sentence, so it’d be more useful to us as a string.

We can use the .join() function to join each value of the list together to make a string.

Before the .join(), we put the string that we want to use to separate the values of the list.

I want to put a space between each value of the list to make a proper sentence.

Inside the parentheses, we put the list.

I can print out sentence, and I’ll get, “Are you learning Python?” as a string.

Let’s talk about tuples. Tuples are very similar to lists.

They contain an ordered set of values like a shopping list or a checklist just like a regular list.

The big difference between tuples and lists is that tuples are immutable, meaning that its values cannot be changed.

Lists are mutable, meaning that list values can be reassigned and changed.

You can create a tuple with the same setup as a list, but instead of square brackets, you use parentheses:

However, even though you use parentheses to create a tuple, you use square brackets to access elements of a tuple.

Let’s say that we wanted to get the first element of this tuple_example.

When we run the program, we get 1, which is the first value of the tuple_example variable.

Now, you might be thinking, why would we use tuples? You can’t change the values in tuples!

Tuples also lack many of the functions that we demonstrated that lists had in the last video.

The main benefit of a tuple is that tuples are faster for constructing a list of values.

If you need to create a list of values that you won’t need to access and manipulate later, using tuples might be for you.

For example, let’s say that we wanted to create a function that swaps two values.

In the function, we are creating a new tuple with x and y swapped with each other.

Tuples are good for quick construction and usage. Tuple construction is faster than list construction.

When we run the program, swapped_values variable will have a tuple with 2, 1.

Tuples, being immutable, are a tad more memory efficient, but it’s almost negligible.

Most of the time, I use lists because there are many situations where I need to manipulate the values of a list.

I would only use tuples whenever I have a simple function where I knew that I would not have to manipulate the values of the tuple beforehand.

If you want a function to return a few values that won’t be changed, feel free to use tuples.


Video Demonstration