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 Basics (2015) Python Data Types Splitting and Joining

Ivica Dimitrijev
Ivica Dimitrijev
1,466 Points

List problem: var_one and var_two

If I write: var_one = ["a", "b", "c"] var_two = var_one print(var_two)

I get: ['a', 'b', 'c']

But if I write: var_one = ["a", "b", "c"] var_two = var_one.extend(["d", "e"]) I get empty list or None

Why is that?

I'm sure it was mentioned in the videos but can't find it.

2 Answers

Stuart Wright
Stuart Wright
41,118 Points

The extend() method alters a list in place, and returns None. So if you say:

var_two = var_one.extend(["d", "e"])

var_one will now have the new elements appended to it (check it out in the console), but var_two will be equal to None, since that is what extend() returns.

An alternative syntax if you want to assign to a new variable rather than change in place would be simply:

var_two = var_one + ["d", "e"]