Now we'll cover a data type that can hold many items inside it. In Python 3 this data type goes by the name list.
About the List Data Type
In Python, a list is a data type that can hold many values. In other programming languages you may know it better as an array.
A list variable is made using square brackets ([...])
>>> nama_buah = ["jeruk", "semangka", "apel", "pisang"]
>>> print(nama_buah)
["jeruk", "semangka", "apel", "pisang"]
>>> type(nama_buah)
<class 'list'>
In that example the variable nama_buah holds several items, namely jeruk, semangka, apel, and pisang.
A list can hold any number of items. Even with no items at all, as long as the square brackets are there, the value still counts as a list.
>>> list_kosong = []
>>> print(list_kosong)
[]
>>> type(list_kosong)
<class 'list'>
There, empty ๐. A list with no items is usually called an empty list.
If it's empty, what's the use? We'll go further into that later.
Accessing Items in a List
Items in a list can be accessed by indexing.
Indexing is done by calling the list variable's name, followed by square brackets with the index number.
>>> nama_buah = ["jeruk", "semangka", "apel", "pisang"]
>>> print(nama_buah[0]) # accesses the first item
jeruk
>>> print(nama_buah[1]) # accesses the second item
semangka
The index in Python starts at 0 for the first item, 1 for the second item, and so on.
If the index you use goes past the length of the list, an error comes out.
>>> nama_buah = ["jeruk", "semangka", "apel", "pisang"]
>>> print(nama_buah[4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
You might be wondering.
"Why does index 4 error out? The list is 4 long, it should print the string 'pisang' ๐ค"
Remember, the index in a list starts at 0 for the first item. Index 1 is used for the second item.
So if we use index 4, the item accessed is the fifth. Because that example only has 4 items, the fifth item can't be accessed.
Changing Item Values in a List
Items in a list behave like variables in general. We can change an item's value.
>>> daftar_angka = [1, 2, 3, 4, 5]
>>> print(daftar_angka)
[1, 2, 3, 4, 5]
>>> daftar_angka[0] = 11 # changes an item value in the list
>>> daftar_angka[2] = 33
>>> daftar_angka[4] = 55
>>> print(daftar_angka)
[11, 2, 33, 4, 55]
In that example we change the value of items 1, 3, and 5 of the variable daftar_angka. You can see the item values are not what they were before.
Adding and Removing Items in a List
To add and remove items in a list, we can use methods Python already provides, namely append and pop.
Method is a function contained in an object. In Python, a list is an object, so it has several methods beyond append and pop.
We'll cover method and object further in the OOP with Python section on another occasion.
Now let's try making an empty list and filling it with the append method.
>>> daftar_nama = [] # creates an empty list
>>> print(daftar_nama)
[]
>>> daftar_nama.append("budi") # adds budi to daftar_nama
>>> print(daftar_nama)
['budi']
>>> daftar_nama.append("inva") # adds inva to daftar_nama
>>> print(daftar_nama)
['budi', 'inva']
Using the append method, we add a new item at the end of a list (the last index)
Then how do you remove an item from a list?
To do that, we can use the pop method
>>> daftar_nama = ["budi", "inva", "siko", "adi"]
>>> print(daftar_nama)
['budi', 'inva', 'siko', 'adi']
>>> daftar_nama.pop(2) # removes the item at index 2
>>> print(daftar_nama)
['budi', 'inva', 'adi']
>>> daftar_nama.pop() # removes the last item
>>> print(daftar_nama)
['budi', 'inva']
The pop method takes an argument, a number showing which index to remove.
If that index isn't filled in, the last (rearmost) item of the list gets removed automatically.
Those two methods are a small sample of the many methods we can use on the list data type. There's also clear, copy, remove, insert, extend, and so on.
Finding the Length of a List
Just like when we looked for the length of a string. A list's length can also be found using the len() function
>>> angka_genap = [2, 4, 6, 8, 10, 12]
>>> len(angka_genap) # gets the length of the angka_genap list
6
That's not all. This one function can actually find the length of every data type that holds many items, such as tuple and dictionary.
Tuple is a data type similar to a list, except its items can't be changed. Dictionary is a list that applies the key and value principle to access its items. We'll cover both further on another occasion.
Wrapping Up
That's it for this one. Hope it helps.
Get ready to say good bye to the terminal and the interactive interpreter. Next time we start real Python programming ๐.
On the next occasion we'll try running Python code by writing a script in a file.