Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Sequences Sequence Iteration Iterating over Ranges

I really don't get his question.

I really don't get his question. What do I need to do with my_list? I don't get the second and third sentence of the question. From the video I understand I need to print the range and use the numbers for it, but what about the append?

iterating_ranges.py
my_list = []

for a in range(0, 10):
    print(a)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,736 Points

Hey Sharon Zonneveld, let’s break it down:

  • 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 (your a variable) to the provided list called my_list. Alist has an append. This modifies a list in place so no = assignment is needed.
  • To append a value to a list, use the syntax my_list.append(val). is an example of how to use the method. In this case, the loop variable is var.

Post back if you need more help. Good luck!!!

After trying a lot and searching on the internet and reading both answers on my question, I found out how to complete this. Thanks a lot for the answer!

Instead of using the print() function, you need to use the append() method.

Right now, you're simply printing the current item to the console. However, instead of printing the item, you need to add it to the my_list - or append it (same thing). At the beginning my_list is empty, but once you run that for-loop with the append method inside, items that you're looping over, get added to the my_list one-by-one.

The 'item' in this case is a number - but can be also e.g. a string or another list.

Every list you declare has the append() method + tons of more! https://docs.python.org/3/tutorial/datastructures.html