Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

5 Answers

Josh Keenan
Josh Keenan
20,315 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 :)

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)
Stanley Nkosi
Stanley Nkosi
4,233 Points

Josh Keenan your answer was the best.

Josh Keenan
Josh Keenan
20,315 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.

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