Table of Contents[Hide][Show]
In this tutorial, we’ll take a closer look at lists. We have already learned about strings and indexes in Python. Let’s dive a little deep.
Lists
Look at this example:
names = ['Shahbaz', 'Aayush', 'Jay', 'Raaj', 'John']
print(names[0])
This will print only the entry at index 0 i.e. “Shahbaz” on the terminal.
Or you can print the complete list just by using print(names)
However, let’s say we have to update the list of names at a later stage of the program. We can do it like this:
names = ['Shahbaz', 'Aayush', 'Jay', 'Raaj', 'John']
names[3] = Dimitri
print(names)
Now the entry at index 3 has been updated to “Dimitri”.
Exercise
Here’s an exercise for you. I want you to write a program to find the largest number on your list. This is a fantastic exercise for beginners, so go ahead and spend a few minutes on this, then come back and continue reading.
Solution
Let’s define a list of numbers
numbers = [6, 7, 2, 0, 9, 4, 1, 3]
Now, to find the largest number in this list, we need to define another variable let’s call it max. This variable will hold the largest number. Initially, you want to assume that the first item in this list is the largest number.
max = numbers[0]
We’re only assuming that the first item is the largest number. Chances are our assumption is wrong. So we need to iterate over this list by looping through it. We will get each item and compare it with ‘max’. If it’s greater than ‘max’, then we need to reset ‘max’ to that number.
for number in numbers:
if number > max:
max = number
print(max)
Two-Dimensional Lists
Let’s talk about two-dimensional lists in Python. Two-dimensional lists are extremely powerfully and they have a lot of applications in data science and machine learning.
Here’s an example: In math, we have a concept called matrix, which is like a rectangular array of numbers. Let me show you.
This is a 3 by 3 matrix having vertical columns and horizontal rows. Now we can model this in python using a two-dimensional list. A two-dimensional list is a list where each item in that list is another list. Let me show you.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
This is a two-dimensional list where each item has a further three items in it. This list will be said to have three items and each item has then three items. This is called a two-dimensional list.
This is how we will access an item within a two-dimensional list.
matrix[1][2]
The first square bracket is representing the item of the first list i.e. [4, 5, 6]. The second one is representing the index of the item inside that list i.e. 6. Let’s print this.
You can also update the individual entries in the program as we did before.
Let’s add a nested loop to make things interesting. This will print all the values in our two-dimensional list on the terminal.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix[1][2]
for row in matrix:
for item in row:
print(item)
List Methods
In this section, we will discuss some of the list methods or list functions. These are the operations that we can perform in a list.
To start off, let’s define a list of numbers:
numbers = [4, 1, 7, 5, 0, 9]
Now, there are a number of things we can do with this list. We can add new items to it, we can remove existing items, and can check for the existence of an item.
So when you type numbers.
a list of different functions will pop up. We can see all the methods or functions you can use with lists.
Let’s call the append method to add a new item to this list. Let’s say ’13’. Actually, no it’s not a good number. Just joking, I’m not superstitious. Let’s add ’13’ and then print our list.
numbers.append(13)
print(numbers)
you can see that the number ’13’ is printed at the end of the list. But what if you want to add a number somewhere in the middle, or at the beginning of our list? For that, we use a different method, which is called an “insert”. It uses two values. The first value is the index where you want to insert an element and the second one is the actual number.
Let’s use it:
You can see that the number ’13’ is printed in the beginning.
Let’s say, we want to remove a number. The method we will use is “remove”.
Let’s check the existence of a number in our list. It is much simpler than it sounds. We are checking the existence of 20 on our list. This is how we will do it:
numbers = [4, 1, 7, 5, 0, 9]
print(20 in numbers)
It will return a boolean value representing the existence of ’20’ in the list.
You can learn about all the different functions from the official Python documentation.
Wrap Up
That was all about the lists. You can experiment with the methods to get proficient in Python lists.
The next lecture will focus on “tuples” in Python.
Leave a Reply