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

Tricia Cruz
Tricia Cruz
915 Points

Why do we use append and extend for lists?

This is probably a silly question, but I just wanted to know why we use append and extend, rather than just going back to the list in your code and editing the elements directly?

1 Answer

Steven Parker
Steven Parker
231,185 Points

If the elements are known in advance (at programming time), you would indeed just add them as additional literal list items. But in many cases, a program will build one or more lists as it runs, using data acquired at that later time.

The primary use of the list-expanding dynamic methods are for creating and maintaining those lists during the program execution.