Simply Basic Python — part 2
So, in this part, we will talk about the powerful bucket named List. If you haven’t read part 1, please go to this page.
Python List
As you can see in the list illustration beside, unlike int, float, and other data types, a list is compound data type. It means that you can store various values with various data types into a list.
listFam = [“Ani”, 11, “Dony”, 8]
The length of a list is so long, you can store a huge of data to them and add any data to an existing list. You can also know its length with method len(listName)
. *p.s. we will learn this in the next story.
All values in the list are separated by commas. The list above shows family member and their age. But, don’t think that list can’t store the same data type at all. You can also create a list just for integer data or float data or other types of data.
It’s also possible if you want to store the value into a variable before you put it into a list.
age1 = 11; age2 = 8
listFam = [“Ani”, age1, “Dony”, age2]
Remember, the mathematic operation rules between the type of variable [part 1] is also applied to the values that you input into the list.
listFam = [“Ani” * 2, 10+1, “Dony”, 4*2]
equal to
[“AniAni”, 11, “Dony”, 8]
List of lists
Surprisingly, a list can store the other list. When you work with huge of data, it makes sense to group some of the data, instead of making flat data containing strings of names and int of their age. You are able to create that list of lists in 3D, 4D, 5D, and so on.
Indexing. Please note that python use indexing from zero (0), so the index of a list will start from 0. And it has special indexing that begins from the end of a list. The last value of a list will be indexed to index -1, then the second value after that will be indexed to index -2, and so on. For example,
listFam = [“Ani”, 11, “Dony”, 8]
index 0
will be“Ani”
,index 1
will be11
,index 2
will be“Dony”
andindex 3
will be8
index -1
will be8
,index -2
will be“Dony”
,index -3
will be11
andindex -4
will be“Ani”
By using that index, you can display the specific value of the list. Learning by example will easier for us. So, please look at this example.
listFam = [“Ani”, 11, “Dony”, 8]
to display specific value, simply print(listName[index])
it will return the value based on its index.
print(listFam[0])
→ return,“Ani”
print(listFam[1])
→ return,11
print(listFam[2])
→ return,“Dony”
print(listFam[3])
→ return,8
How about 2D, 3D, .. list, list of lists?
It’s pretty easier than you think. Look at the list of lists illustration again. Just think that the list of lists it’s the only flat list that contains a list on each index. Then move your attention to the list on each index. It also has its index. So, you can display a 2D list or list of lists by simply writing the two indexes of them. One for the outer list and one for the inner list. Below, is an example for a list of lists based on the illustration above.
print(listFam[0][1])
it will return11
print(listFam[0][0])
it will return“Ani”
print(listFam[1][1])
it will return8
and when you
print(listFam[0])
it will return[“Ani”, 11]
If you feel it’s too complicated, look at this illustration.
Arrange each index like that, so you can imagine that list has rows and columns. Then, to display the value, you can print(listName[row][column])
.
“Dony”
on row = 1, and column = 0 (please, always remember indexing in python start with 0). So, you can retrieve the value “Dony”
with listFam[1][0]
. And also for the other value.
Slicing. That’s all the methods to retrieve value one by one. But, it’s possible to slice the list or I mean retrieve multiple values of a list. To do this magic, please remember this rule correctly.
Based on that rule, you can select multiple values of a list. For example,
listFam = [“Ani”, 11, “Dony”, 8]
we will retrieve index 0 to index 2
. So, we do:
print( listFam [ 0 : 3 ])
→ return:“Ani, 11, “Dony”
0 will be included and 3 will be excluded.
print( listFam [ 1 : -1 ] )
→ return:11, “Dony”
However, it’s possible not to specify these indexes. If you don’t specify the beginning index, python will slice the list start from the beginning of your list. If you don’t specify the end index, the slice will go all the way to the last element of your list. Look at this example:
listFam = [“Ani”, 11, “Dony”, 8]
print( listFam[ : 2 ] )
→ return:“Any”, 11
print( listFam[ 2 : ] )
→ return:“Dony”, 8
print( listFam[ : ] )
→ return:“Ani”, 11, “Dony”, 8
After you extracted the element of the list, you can use them to perform calculations. But, you have to keep paying attention to the calculation rule between data type of value. For example,
listFam = [“Ani”, 11, “Dony”, 8]
print(listFam[0] + listFam[2])
→ return:“AniDoni”
print(listFam[0] +listFam[1])
→throw an error
, because you tried to add integer into string
print(listFam[1] + listFam[3)]
→ return:19
Replace. If you have a list and you just realized there was a typo, don’t worry, you can replace your typo value on the list. You can select a single element or change entire list slices at once.
listFam = [“Ani”, 11, “Dony”, 8]
listFam[0] = "Anik"; listFam[3] = 10
→ changeindex 0
to"Anik"
andindex 3
to10.
Result:listFam = [“Anik”, 11, “Dony”, 10]
listFam[:2] = ["Ani johnson", 15]
→ changeindex 0 to 1
. Result:listFam = ["Ani johnson", 15, "Dony", 10]
Extend a list. As a basis, we can extend a list without using a method. Simply using +
operator.
listFam = [“Ani”, 11, “Dony”, 8]
newList = listFam + ["Carl", 20]
→ return:newList = [“Ani”, 11, “Dony”, 8, "Carl", 20]
Delete. You can remove elements of the list by using del
statement.
newList = [“Ani”, 11, “Dony”, 8, "Carl", 20]
del(newList[1])
→ result:newList = [“Ani”, “Dony”, 8, "Carl", 20]
Remember, as soon as you remove the element of a list, the indexes of elements that place after deleted element all change. So, in the example above, "Dony"
has index 1
replace element11
‘s position.
EXTRA: How Python lists work
You create a list named listA.
listA = [7,4,12,1,3,2]
What actually happens when you create that list?
well, in a simplified sense, you’re storing the list at the computer memories, and storing the ‘address’ of that list in listA. So this means, listA doesn’t actually contain all of the element lists. It’s just contains reference / ‘address’ to the list.
So, when you copied listA
on listB
by =
operator, like this listB
= listA.
It means that you only add other references to the list.
The effect is when you change listB
it’s also changing listA
because they referred to the same list.
to copy all elements of the list to the new list, you can use list()
method or [:]
statement.
listA = [7,4,12,1,3,2]
listB = list(listA)
orlistB = listA[:]
so, when you change listB
, it has no impact to listA
.