13/18 - Modifying each element in a list in a function


The code below is correct, my question is: Why is the return x indented back to the same spot as the for i in range, instead of on the same indent as x[i] = x[i] * 2.

n = [3, 5, 7]

def double_list(x):
  for i in range(0, len(x)):
    x[i] = x[i] * 2
  return x
# Don't forget to return your new list!

print double_list(n)


By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends

so if you place return inside the loop, the function would end in the first iteration of the loop


Do you use two spaces instead of ()?


who are you asking? I do not understand what you are referring to


It was question for topic starter, he modified post and it’s clear now.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.