198k views
0 votes
Write a function named square_list that takes as a parameter a list of numbers and replaces each value with the square of that value. It should not return anything - it should mutate the original list.

1 Answer

3 votes

Answer:

The function is as follows:

def square_list(myList):

for i in range(len(myList)):

myList[i] = myList[i]**2

print(myList)

Step-by-step explanation:

This defines the function

def square_list(myList):

This iterates through the list elements

for i in range(len(myList)):

This squares each list element

myList[i] = myList[i]**2

This prints the mutated list

print(myList)

User Phanf
by
4.6k points