We’ve looked at different ways to working with lists using functions and built in methods of the list. But now I want to show you how you might use Python to work with a list that doesn’t use a normal command. Some will be using loops, and operators, others, like copying a list to a new list, requires a little bit of a trick.
Copying Lists
We’ve previously used the equal sign (=) to copy values and other variables into a variable. For example x = 5, y = x. Both x and y hold the value of 5. However, if we were to say x = 13, y would still equal 5 as the value was duplicated and while identical, it was wholly different.
x = 5
y = x
print("x:", x)
print("y:", y)
x = 13
print("x:", x)
print("y:", y)
If you do the same thing to two lists however, the list variables still reference the same list. A change the one value of one element is a change to the other variables elements, because they are the same. This is called a shallow copy, and is done by default for several reasons – but basically it comes down to two things given that you could have hundreds or even thousands of elements, doing a shallow copy is both faster and less memory intensive.
x = [1,2,3]
y = x
print("x:", x)
print("y:", y)
x[1] = 5
print("x:", x)
print("y:", y)
With variables where there is a single value, the speed and memory requirements are basically identical. So they always get a deep copy.
But if we wanted to do a deep copy, we would have to write a loop to copy each element into the new list, one by one. Or we could use a short cut.
x = [1,2,3]
y = [] + y
print("x:", x)
print("y:", y)
x[1] = 5
print("x:", x)
print("y:", y)
Because “y” is concatenating an empty list, with the list stored in “x”, it creates a new copy for us. While this will take longer than a shallow copy, these two lists are completely independent of one another which is needed in some circumstances.
Finding the List Total
Adding items to a list, like the individual sales totals, or inventory counts is a common thing to do. Then when you do, you might need to tally them up at the end of the day, inventory process, etc. to get a total, or the sum of the list.
Likewise, if you are finding the average, you’ll need to know the total to calculate it.
Unfortunately, there isn’t a summing, or total function for us to use. So we’ll need to write this utility ourselves.
numbers = [2,5,7,12,3,19]
total = 0
for value in numbers :
total += value
print("The sum of the list is:", total)
Calculating the Average
If I had a list of test scores, I would most likely need to find the average. To do so, I can use the previous section to determine the sum of my list, and then divide that by the number of items in the list.
grades = [82,85,79,92,93,89]
total = 0
for grade in grades :
total += grade
avg = total / len(grades)
print("The test average:", avg)
Notice how we use the len function from before to get the count of items in the list.
Passing List to a Function
You might think to yourself, that a calculating the sum and average would be good to do in independent functions. That way you can use them over and over again throughout your program. And that would be good thinking. So let’s look at how to pass a list to a function.
Passing a list to a function, is just like passing any other variable. However, you want to remember that this will be a shallow copy. Something worth remembering in case you plan to temporarily change a list item, it will be reflected back in the original part of the program.
def total(values):
total = 0
for val in values :
total += val
return total
def average(values) :
sum = total(values)
return sum / len(values)
Notice how in our average function, we call our total function, and get it’s return value to help us calculate our average. This is all part of code reuse and not wanting to have to write any more code than necessary. Passing values, and using return values is a great way to extend the use of a function.
Notice how we don’t print anything in these functions. If we did, it would limit how we could use them. Instead, we just use them to process our list, and then we get a result back which we can use later on as we need to. Potentially even in different ways.
Working with Lists in Python was originally found on Access 2 Learn