39

Which is more pythonic?

list.append(1) 

or

list += [1] 

8 Answers 8

76

list.append(1) is faster, because it doesn't create a temporary list object.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 - Both the answer and explanation are good. Succinct and to the point. Thanks.
Wasn't the question "which is more pythonic?"
List object creation is beside the point. Besides, (:-) CPython will reuse a list from its empty list in this case.
@ojrac: What is faster is often what is more Pythonic, as the Python devs tend to optimize behaviors they encourage.
And "which is more pythonic?" was the question. The word "faster" is totally off-topic unless there's a serious issue with either approach, which there is not.
|
55

From the Zen of Python:

Explicit is better than implicit.

So: list.append(1)

4 Comments

is list += list not explicit?
IMO, operators are less explicit function names.
As an example for operators being less explicit than named functions: what does 'list += list' mean? Reading it, i could think appending, but I could also think you might want to add the values in a list of numerical values (e.g. [1, 2, 3] += [4, 5, 6] => [5, 7, 9] could make sense).
You might also think that [1,2] += [3] => [1,2,[3]]
18

These are two different operations, what you are doing with += is the extend operation. Here is what Python documents have to say about this:

list.append(x): Add an item to the end of the list; equivalent to a[len(a):] = [x].

list.extend(L): Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.

So in += you provide a list, in append you just add a new element.

Comments

8

While most people here are preferring the append option, I personally prefer the other one because it looks nicer even though it may be slower (or maybe its optimized).

Beautiful is better than ugly.

When you write lots of Python code, I don't usually see something like this:

list.append(1) 

It's more like this:

somecollectionname.append(anotherelementname+5*10) 

So to me at least, it is nicer to see:

somecollectionname += [anotherelementname+5*10] 

Because its easy to recognize at a glance that you are adding to a list. Having said that, I sometimes find myself using both forms.

Comments

7

Since there's also

list.extend(l) 

which appends all elements of the given list, I would use

list.append(1) 

for symmetry and readability's sake.

1 Comment

I personally keep confusing myself with the difference between extend() and append(). Therefore, had the question been about some list lst rather than a fixed single element 1, my answer would have been: += is better because then I am sure the left and right operand are peers and we are talking about a concatenation here, not an add the whole list as the last element.
4

list.append(1) more readable and to consistent with the context

Comments

4

If you've got a single element, a, that you want to add to your list l, then putting a into its own list and extending l with it seems like adding unnecessary complexity. I would thus prefer

l.append(a) 

over

l += [a] 

If a is already a list, then choosing

l += a 

or

l.extend(a) 

is a matter of preference, IMO. On the other hand, if you're going to be doing a lot of extends, you can get a performance boost by "hoisting" the method lookup:

extend = l.extend for sublist in bunch_of_lists: extend(sublist) 

Finally, I think that the append operation isn't used too often in Pythonic code, because append is used very often in "accumulator" idioms, where I'd expect a more experienced Python programmer to use a list comprehension/etc.

So instead of:

l = [] for a in numbers: l.append(str(a)) 

You'd probably see:

l = [str(a) for a in numbers] 

or

l = map(str, numbers) 

1 Comment

You surely you map(str, numbers) ?
1

Beware of fundamentalism

Have you noticed the term "more pythonic" in your question? I think it implies that you are asking a not-so-helpful question.

If one is merely more pythonic than the other (rather than being pythonic while the other is not) it could be said that you have violated the Zen of Python on a meta level: "Simple is better than complex" should also hold for the process of searching for the form in which you express your logic -- once you have found something pythonic, that is good enough. Keep the search simple.

So my answer would be: Neither of them is more pythonic. The most pythonic thing is to go on and write a nice program.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.