Lists in Python - Native Data Handling

Lists are used to store and manage collections of data. Let’s explore this important topic while solving a little problem.

Sari Lakkis
9 min readMay 24, 2021
Photo by Valeriy Kryukov on Unsplash

We’ll learn this topic while solving a little problem, also the problem will cover other previous topics like loops to practice them more.

Our problem for this tutorial is to calculate the average and maximum temperature in a certain week. We have the daily temperature measurements of a week. We also want to output the lowest three temperatures.

First of all, we want to store these values somehow and then calculate the average and the maximum temperature. We will do that without using built- methods of the lists. Then we will find the lowest three temperatures while learning some of the important methods.

How can we effectively store these temperatures and other forms of data with Python? One way to do that is using a list, let’s take a look at lists in Python.

The Source code of this article can be downloaded from my GitHub Python repository.

Let’s start!

Why Do We Need Lists?

If we want to store today temperature, for example, we can simply say

temperature = 45

So, we can a variable for each day to store the temperature reading in it, then we can add another variable temperature1 for tomorrow’s temperature and so on. We end up having a lot of variables.

What if we want to store the temperatures of the whole year or even many years can we keep doing that? of course, this won’t be practical. Also, we’ll have to write expressions that will include all these variables, like:

Sum = temperature1 + temperature2 + … + temperature365

Which is not the greatest way to code, right?

One answer to these kinds of problems in Python is to use lists.

Lists in Python

Lists or similar data structures in Python allow us to store a collection of data with one variable reference for example, in our case where we are storing many temperature values.

To write that we simply put the values inside the square brackets, for example, we have the following list with four elements:

temperatures = [12, 45, 39, 18]

We created a list called “temperatures” that contains four elements, the values inside the list are ordered with indices that start from 0 to 3.

Elements in a List Can Have Any Type of Data

We can create lists with any type of data like strings, Booleans, and so on, or even you can mix types inside of a list but why would you do that?

For example, we have this list named currencies:

currencies= ["EUR","USD","CAD"]

Accessing Data Inside a List

If we want to access a certain value, we can by knowing its index, for example, to print the first element, which is 12, we write:

print(temperatures[0])

Or the second element we write temperatures[1] and so on.

Why do we count from zero?

We don’t, in most programming languages, indices start from zero. So, the first item in a collection of items is number zero but it’s counted as one, and the second is number one and the count is two. This has to do with how memory is organized and referenced.

Accessing the last element

Another great feature in Python is that we can reference the last index with -1. Also, the other elements in the list are in reverse order with negative index values.

This means that if we want to access the last element, which is 18, we can write:

print(temperatures[-1])

Also the element before the last one (39) by writing temperatures[-2] and so on.

This gives us a lot of flexibility while dealing with lists. We can use a variable as an index which gives us the flexibility in iterating through them as we’ll see in our problem.

We are using print here just to demonstrate accessing the value but we can do anything we want with it like including the elements in expressions.

If we want, we can print the whole list by referring to its name:

print(temperatures)

This will print all the values with square brackets: [12, 45, 39, 18].

Changing values of elements in a list

We can also change the values of the list elements, add new elements or remove them according to our application requirements. This is important to note that lists are mutable, which means that they can be changed.

For example, suppose that we need to change the value of the second element (at index 1) to 23, we can simply do that by:

temperatures[1] = 23

Solving Our Problem

Back to our problem, since we now know how to store the temperatures in a list.

As a recall, create a folder called “temperatures” and open VS Code inside it. Create a file named “main.py”.

Inside “main.py” lets’ create our list that contains the temperature of the whole week, so we have a list with seven elements.

temperatures = [30, 34, 32, 18, 10, 20, 8]

To calculate the average, we can use either a while loop or for loop but as we’ll see the for loop is much easier when we deal with similar data structures.

Computing the average using while-loop

As we saw, the index can be a numeric literal or a variable, for example, if we have a variable “i” with value 2 we can access temperature at position “i”.

i=2
print(temperatures[i])

To add all the numbers in the list we can create a loop with variable “i” from 0 to 6 that adds all the values in the list at the indices from 0 to 6 and store the result in a value called “total”.

Then after the loop ends, we divide “total” by 7 and to get the average temperature.

We’ll use the “+=” operator which adds to the previous value of the variable the new value and assign the result to it, so instead of writing:

i = i + 1
#we can write
i += 1
temperatures = [30, 34, 32, 18, 10, 20, 8]
sum = 0
i = 0
while i < 7:
sum += temperatures[i]
i += 1
average = sum/7
print("The average is " + str(average))

If we don’t want to use number literals like 7, which is not a good practice in writing clean code, we can define a variable list_length and also assign it with list length using the function len() which return the length of the list:

temperatures = [30, 34, 32, 18, 10, 20, 8]
list_length = len(temperatures)
sum = 0
i = 0
while i < list_length:
sum += temperatures[i]
i += 1
average = sum/list_length
print("The average is " + str(average))

What if we indexed an element outside the range of the list?

We’ll get this runtime error if we try to access an element outside the list.

IndexError: list index out of range

Solving the problem with a for-loop

For example, to iterate over the temperatures list we write:

for t in temperatures:

So now each temperature is referenced by the variable “t”.

Replace the code with the following code:

temperatures = [30, 34, 32, 18, 10, 20, 8]
sum = 0
for t in temperatures:
sum += t
average = sum/len(temperatures)
print("The average is " + str(average))

This is much simpler than the while loop since we don’t have to index each element in the list.

Determining the maximum temperature

As you see we have all the values unsorted inside the list, so the only way to determine the maximum one is to scan the whole list.

We will define a variable “max” to hold the maximum value, we’ll suppose that the first element is the maximum.

max = temperatures[0]

Then while we scan the list, we’ll check every element if it’s greater than our max variable. If so then we have a new max and we assign max with the element value. If not, we don’t have to do anything.

for t in temperatures:
if t > max:
max = t

After we are done, we have the maximum value stored in the max variable so now we can output it.

We can write another loop for that or take advantage of the loop we’re already using to compute the average, let’s do that and save some space:

temperatures = [30, 34, 32, 18, 10, 20, 8]
max = temperatures[0]
sum = 0
for t in temperatures:
sum += t
if t > max:
max = t
average = sum/len(temperatures)
print("The average is " + str(average))
print("The maximum is " + str(max))

A little homework for you, can we also get where the maximum value was found? At what index? This would be useful since we can also link it to the date as our list index represents a day of the week. I’ll leave that for you to do it.

Hint: use a while loop.

Finding the lowest three temperatures

Up till now, we have used lists as arrays in other programming languages like Java or C with a little bit of difference in the syntax of course. Now we’ll explore more about lists that make them special and important.

To find the lowest three temperatures using the same technique we used for finding the max is not quite easy. We’ll use another algorithm.

How about sorting the list from smaller to larger values (ascending order), then picking the top three values. Seems better right?

Sorting a list

To sort a list we’ll use a built-in method sort(), the method by default sorts the list in ascending order. Let’s try to sort and print the list after sorting.

temperatures.sort()
print(temperatures)

We’ll get the list sorted printed: [8, 10, 18, 20, 30, 32, 34]

In case we wanted the sort to be in descending order we can write:

temperatures.sort(reverse=True)

But, there is a little problem here. The order of elements was changed and we don’t want that. What is the solution?

Let’s make another copy and leave our data safe.

Copying a List

If we want to make another copy of the list temperatures we can’t simply say:

copy_list = temperatures

This will only make another reference to our list called copy_list with the same elements in memory and will not create another list.

So if we modify elements with one of the variable references, the same change is seen in the another since the two variables (copy_list, temperatures) reference the same list.

Clear one of them using clear() method and print the other one, it will also be cleared.

copy_list = temperatures
copy_list.clear()
print(temperatures)

This is an important thing to know and we’ll get to why when we explore objects in future articles.

To make a copy of the list, we can use the copy() method or we can write our code to copy element values one by one. To use the copy method, we write:

copy_list = temperatures.copy()

Now the two lists (copy_list and temperatures) are independent and if we modify one of them the other will not be affected.

Now we are ready we can sort the copy_list and print the first three elements but let’s be more complicated. Let’s make another list to hold our lowest temperatures.

To add the elements to the new list we’ll use the method append(). We’ll also use a for loop to add the elements from copy_list to top_list at the indices from 0 to top which is three in our case.

top = 3
top_list=[]
for t in range(top):
top_list.append(copy_list[t])

We may also just delete the other elements form copy_list using the pop() method. Try to do that as a little exercise.

The complete code

temperatures = [30, 34, 32, 18, 10, 20, 8]
max = temperatures[0]
sum = 0
for t in temperatures:
sum += t
if t > max:
max = t
average = sum/len(temperatures)
print("The average is " + str(average))
print("The maximum is " + str(max))

copy_list = temperatures.copy()
copy_list.sort()
top =3
top_list=[]
for t in range(top):
top_list.append(copy_list[t])
print("The lowest three temperatures are: " +str(top_list) )

Conclusion

In this tutorial we explored lists in Python, we saw why we need them and how to use them. We discussed how to access data in a list, how to manipulate it with loops, and used some of the important methods like copy and sort.

There are other types to deal with a collection of data in Python like the set, tuple, and dictionary. We might look at some of them in future articles.

Methods and functions are great, we used them and saved a lot of time. In the next tutorial, we’ll see how to create our own functions. See you there!

--

--

Sari Lakkis

Professor | Consultant | Researcher. I give advice on strategy, design, and implementation of software that solves business problems to companies and startups.