Python Python Sequences Sequence Iteration Iterating over Ranges

Write a for loop that iterates over a range with a stop value of 10 (assume the start value is 0 and the step value is 1

Write a for loop that iterates over a range with a stop value of 10 (assume the start value is 0 and the step value is 1). The body of the for loop should append the current value to the provided list called my_list. To append a value to a list, use the syntax my_list.append(val).

iterating_ranges.py
my_list = range[10]
for val in my_list:
    my_list.append(val)
    return val

4 Answers

Josh Keenan
Josh Keenan
19,393 Points

You don't need a return statement, a return statement is only used within functions or methods.

my_list = []
for i in range(10):
    my_list.append(i)

This is my solution, it iterates 10 times and adds the current loop count to the list.

Stanley Nkosi
Stanley Nkosi
4,233 Points

Thank you very much Josh.Your solution was spot on ! I can go to sleep peacefully tonight.

Tried it first, then it gave me a red message. Discovered that I needed to keep those brackets empty. Thank you Josh, this passes the challenge very well :)

Stanley Nkosi
Stanley Nkosi
4,233 Points

Josh Keenan your answer was the best.

Josh Keenan
Josh Keenan
19,393 Points

You can click the best answer button to mark it as such, and glad I could help. You got this!

Stanley Nkosi
Stanley Nkosi
4,233 Points

Josh your solution was the best answer.

jon nikolakakis
jon nikolakakis
1,635 Points

Thanks Josh Since question asked for val, it could also look like this. With 0, starting the range at 0 and increment to 1 Or using default

my_list = []
for val in range(0,10,1):
    my_list.append(val)

or

my_list = []
for val in range(10):
    my_list.append(val)