13/18 Modifying Each element in a list in a function


Hello, I would like to know how this code works. I found it on another thread which leonard wrote the solution for this problem.

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

print double_list(n)

Can some please explain this code to me one by one. I wanted to learn more about this list lessons and I am trying my best to understand.


n is list, def double_list is function that takes one argument x, d_list is empty list
you iterated over in range from 0 to length of x and appended what you iterated and multiplied that with two, to you empty list, returned your d_list, called function at the end


Thank you, I have understand it, but your explanation is a bit not satisfying for me.


If you want a really good breakdown line by line, I recommend using Python Tutor. I use this website all the time and love how it works, just put in the code and it literally breaks it down line by line and explains what is happening.