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

Mo Reese
Mo Reese
11,367 Points

Getting back a "None"

I passed the challenge, but became confused when the editor refused to show me a preview & told me to 'check my code'. When i went to work spaces, all I got was: None None None... (x10) When I thought I would get [0, 1, 2, 3,etc].

my_list = []

for i in range(0, 10, 1):
    print (my_list.append(i))

Can someone please tell me what I am missing & why i did not get the expected result of a list with int 0 thru 9?

2 Answers

Hey! I thought this would give me the list too when I first looked at it. The trouble is that

my_list.append(i)

is a NoneType, meaning that it adds (i) to the list, but isn't the list itself. So printing that statement won't give anything other than None.

To get the full list, you could just separate the commands.

Hope this helped!

Mo Reese
Mo Reese
11,367 Points

Ok, I see what you're saying about separating the commands, and when I ran:

my_list = []

for i in range(0, 10, 1):
    my_list.append(i)

print (my_list)

I did get the result that I expected... Thanks!

From the docs

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.

Mo Reese
Mo Reese
11,367 Points

Thanks for your response Kris. I'll use that link in the future!