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 vs extend (Shopping list)

Why is it that when i use append in the shopping list i get the list word by word as opposed to extend where I get individual letters of the word in a vertical manner. Does this happen all the time when I use extend. And if it doesn't why can't I use extend in place of append in the case of shopping list.

Any input would be much appreciated. Thank you

Can you post your example code perhaps? It might make it easier to understand your problem.

Usually, append is used to add a new item (such as a string) onto the end of a list, whereas extend is used to concatenate two lists into one list.

The less common uses are as follows (it sounds like you might be doing the second one):

If you use append to try to combine two lists, the appended list becomes a nested element of the original list.

If you use extend to try to add a string to a list, it will break that string down to single characters and extend the list to include those.

1 Answer

When I use extend: (isn't extend used to add two or more items at the end of the list? But this is something new for me where the word is split up into individual letters.

treehouse:~/workspace$ python shopping_list.py
What should we pick up at the store?
Enter 'DONE' to stop adding items.

apple
pear
ted
DONE
Here's your list:
a
p
p
l
e
p
e
a
r
t
e
d

When I use append:

treehouse:~/workspace$ python shopping_list.py
What should we pick up at the store?
Enter 'DONE' to stop adding items.

pear
apple
ted
DONE
Here's your list:
pear
apple
ted