5.8. Lists¶
5.8.1. Accessing elements, lists and for loops¶
The built-in function range
can be used to create a long list of integers as in the following program:
a = list(range(1,500))
print(a)
Create a program (save it as li_intro.py
) that:
creates a list (using the
range(1,500)
statement),uses a while loop traversing the list, printing each element, and its square root,
uses the same while loop to calculate the sum of the square roots of the elements, and prints the sum.
It is recommended to use a shorter list first (when developing the program), e.g. range(1,5)
. But note that the question below refers to the list created with range(1,500)
.
Question What is the sum of the square roots of the elements?
9201
8723
6310
7442
Correct answers: d.
Feedback:
import math a = list(range(1,500)) print(a) sum = 0.0 i = 0 while i < len(a): v = a[i] vSqrt = math.sqrt(v) print(v, vSqrt) sum = sum + vSqrt i = i + 1 print("The sum of the square roots of the values is", sum)
5.8.2. List deletion¶
Create a program (name it li_delete1.py
) that creates a list with 5 elements of type floating-point. Now, extend the program such that all elements smaller than a threshold value are removed from the list.
This can be solved by traversing the list using a while statement, but this may cause problems. As you will know, deleting an element in a list can be done using del
. This results in removal of the element and consequently, the list becomes one element shorter. This will cause a problem: it is not possible to go through a list using a for
loop and delete elements from the same list simultaneously because in that way the for
loop will not reach all elements. A similar kind of problem happens when you try to add lists elements while traversing the same list. The solution is to create an empty list, and copy the elements that you want to keep to that clone list.
Approach the problem in a stepwise manner as follows:
use a
while
loop to print only the list elements smaller than or equal to the threshold value, test the program,above the
while
loop, create an empty list (e.g.newList
),in the
while
loop, append the list elements smaller than or equal to the threshold value to this empty list. For each element, append it to list. This can be done withnewList.append(a)
, wherea
is the value that needs to be appended.
Question How can you create an emtpy list?
Using
[]
Using
a.emptyList()
Using
range(0)
Correct answers: a.
Feedback: FeedbackText
a = [100.9,34.0,34.2,7.8,9.0] print(a) # empty list aNew = [] i = 0 while i < len(a): if a[i] >= 10: aNew.append(a[i]) i = i+1 print(aNew)
5.8.3. Matrices and gridded maps¶
Write a program matrix.py
that prints the nested list:
matrix = [[1,2,3],[4,5,6],[7,8,9]]
formatted like this:
1 2 3
4 5 6
7 8 9
Question An element from this nested list matrix
can be accessed through:
matrix[1,2]
matrix{1}{2}
matrix[1][2]
Correct answers: c.
Feedback:
matrix = [[1,2,3],[4,5,6],[7,8,9]] i = 0 while i < len(matrix): j = 0 while j < 3: print(matrix[i][j], end=' ') j = j + 1 i = i + 1 print()
5.8.4. Strings and lists¶
Write a program that asks the user to enter two numbers separated by a comma, printing the product (the number resulting from multiplication) of the two numbers. Save it as multi.py
.
Question You can use a string method (from str
). Which one?
split
replace
lower
count
Correct answers: a.
Feedback:
numbers = input("Enter two numbers separated by a comma ") # split the string using ',' as a separator, resulting in a list numbersList = str.split(numbers,",") firstNumber = float(numbersList[0]) secondNumber = float(numbersList[1]) product = firstNumber * secondNumber print("The product of", firstNumber, "and", secondNumber, "is", product)