Let’s jump to lists. Perhaps our first real data structure and one of the most used in many programming languages.

Reading

Pretty light this time but to introduce you to one of my favourite writers in tech and life in general. Now his really good ones are pretty long and you’ll fully appreciate them at a later point but here is a short one explaining an interesting effect of Python vs Java

Lists

Lists are basically how we represent a list of items as a single entity in a program. This is a very powerful and useful concept, and the first time we are looking at how to group and organize data. Lots of programming problems are solved very neatly and efficiently after we know how to group and represent the data just right.

Why lists

Say I want to get the class average of two math students.

first_student_score = 80.0
second_student_score = 70.0

average = (first_student_score + second_student_score)/2
print(average)

Which works fine, add them up and divide by number of students (here 2).

Now say I had a class of 10 (don’t even bother typing this)

first_student_score = 80.0
second_student_score = 75.0
third_student_score = 43.0
.
.
.
tenth_student_score = 98.0

average = (first_student_score + second_student_score ... tenth_student_score)/10.0
print(average)

Yikes, that does not look fun. Now imagine I added another student. Or I had a class of thousand, the program will get to massive sizes and be a mess to maintain.

Instead we use lists as a nice way to just keep a general list.

student_scores = [80.0, 43.5, 78.5, 98.5, 50]
sum = 0
for score in student_scores:
    sum += score

average = sum / len(student_scores)
print(average)

That example may look complicated to you but this code is drastically better than what we had above. Because now it’s using a list I can easily add more and the code still holds up. It’s generalizable so it’s better. Programs that can handle a larger variety of situations are generally a good aim to follow. Sometimes they are not and you will get the sense of that later.

Again like last time, spend some time reading that and trying to understand how that program works before breaking it down.

Take a moment.

Let’s go.

Declaring

student_scores = [80.0, 43.5, 78.5, 98.5, 50]

This is the list declaration. We add [ to start it and ] to end it, and in there we enter the values seperated by numbers. An empty list declaration looks something like

empty_list = []

The ‘outside variable’

sum = 0

So sum is what we’re trying to calulate. Often when you are trying code like this you have to declare the variables you care about outside the code. You’ll get a sense of this once

Looping over them

for score in student_scores:

For loops are a great way of looping over a list’s contents. So here the loop will be run for each element in student_scores and score would change to be each value one by one. First round it will be 80.0, next round it’s 43.5 and so on.

Getting the size

average = sum / len(student_scores)

To get the average we have to divde by class size. To get the size of a list in python we use len() - similar to strings.

Exercise: Max of a list

Let’s drill this down concepts so far before we try more. Write a program that takes the same student_scores we were using earlier but now it finds the highest score (perhaps because we want to give in award). Write the program in a way that it can work for any list though.

The max of the list is 98.5

Accessing elements

Consider the list

fav_numbers = [1, 2, 5, 25, 42, 2520]

Now if I want to print out the first one. I say

print(fav_numbers[0])

To print the second

print(fav_numbers[1])

and so on.

Note: Counting in Python (and most programming languages) start from 0. It’s sure confusing but you get used to it suprisingly quick. Lots of good programmers I know even use the term zeroth in daily language.

If you want to access the elements from the other end.

>>> squares = [1, 4, 9, 16, 25]
>>> squares[4] # Last element
25
>>> squares[-1] # Better way to get last element
25
>>> squares[-2] # Second last element
16

So -<index> basically says give me these many spots off the back. You can think of it like a clock. If 0 is the start, then -1 is basically going all the way around to the previous one which is 11 o clock in real life. Likewise in lists it basically comes in from the other end.

Writing elements

Once you know how to access elements writing to them is just simply using an = like you did with variables. Here is an example.

>>> squares = [1, 4, 9, 16, 25]
>>> squares[2] = 36 # Just use an equals to change whatever
>>> squares
[1, 4, 36, 16, 25]

Append

Adding to the end of a list is quite simple, using append() by attaching a it to the array name using a dot . you can add more to the end.

>>> squares = [1, 4, 9, 16, 25]
>>> squares.append(36)
>>> squares
[1, 4, 9, 16, 25, 36]

Pop

Pop does the opposite of what append does, removing the last element. It also returns the element it removed, so you can use this as a way to access the last element and remove it and store it in a variable or print it.

>>> squares
[1, 4, 9, 16, 25, 36]
>>> squares.pop()
36
>>> squares
[1, 4, 9, 16, 25]

Insert

If you want to add an element in some position and have everyting else shift to the right it’s also quite simple. Look at this example here.

>>> numbers = [1, 2, 4, 5]
>>> numbers.insert(2, 3) # Oops forgot to add 3, let's add 3 at position 2
>>> numbers
[1, 2, 3, 4, 5]

Vs arrays

Now you might be familar with Java or C style array and wonder why I did not call these arrays, but the key difference in basically that arrays are of fixed size and lists are not. In Java the exact version of this go by List, and in C++ std::vector are similar to what we covered here.

They have their own advantages, and in python they are called tuples but that we will cover later.

And more?

Lists have many, many features in python (and other languages), and I’ve only give you the most important ones you need for basic programs but the rest are quite interesting and you will explore more on the way.