Why would I user append if I can just use +?

Hi,

I just learned how to use + to combine lists. Great.

What is the difference in using append to add one item to a list and using + [variable]? Can’t I just use + in all cases?

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/create-python-list/exercises/plus-list

then you would first need to create a new list, then you combine two list, its sounds like less efficient. I would use the right tool for the right job.

1 Like

+ and append do different things. append adds one item (if that is a list then it will add the whole list as one item, it doesn’t flatten it) while + will concatenate the lists into one list. The comparable to + is extend.

As @stetim94 said, + will create a new list, while extend (or append for that matter) will mutate the original list. So + will use more memory so will be more inefficient in that sense if you did, but it may actually be fractionally quicker if you did:

lst = [1,2,3]
lst = lst + [4,5,6]

So why use + and not extend? When you want to conact two lists but not mutate either list but rather have a whole new list. If you actually want to change the original list then use extend.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.