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

Append and Assign to a variable - Python

Need explanation. Please look the below code:

---------> Start.

family = ['Daddy','Mummy']

family ['Daddy', 'Mummy'] family.append('Child') family ['Daddy', 'Mummy', 'Child']

  • This works fine as intended.

The Problem: If i try to append second child to the family list and have it assigned to the same variable or different variable, nothing happens. Example:

family = family.append('Second Child') OR Totalfamily = family.append('Second Child')

Then, try to find the contents of family OR total family, nothing happens? Why please?

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points
family.append('Child') 

This appends 'Child' onto family.

family = family.append('Second Child')

This appends 'Second Child' onto family. But the append method doesn't return anything (so it returns None), it doesn't return the list as you might be thinking. So then what happens is the result (None) gets assigned to family, and now family is empty.