标签云

微信群

扫码加入我们

WeChat QR Code

What's the difference between the list methods append() and extend()?


What is the difference between extend and simply using the addition operator - in the above example, x = x + [4, 5]?

2019年05月24日55分39秒

Actually there's a big difference - x + [4, 5] gives you a new list assigned to x - x.extend() mutates the original list. I elaborate in my answer here below.

2019年05月24日55分39秒

AaronHall Rohan but it is same as x += [4,5].

2019年05月24日55分39秒

AstitvaSrivastava Actually, I think extend is faster in terms of bytecode

2019年05月24日55分39秒

The keyword when using append is Object. If you try to use extend and you pass in a dictionary, it will append the key, and not the whole hash to the end of the array.

2019年05月24日55分39秒

Perfect answer, I just miss the timing of comparing adding only one element

2019年05月24日55分39秒

Aaron Hall One small comment in algorithm for timing. "extend_one" may return "slightly wrong" time because the creation of a list is also involved. Probably better is to create the items as variables (ex1 = 0 and ex2 = [0]) and pass these variables, if you want to be more strict.

2019年05月24日55分39秒

Perfect answer indeed. What about performance of l1 += l2 vs l1.extend(l2)?

2019年05月24日55分39秒

Jean-FrancoisT.: l1 += l2 and l1.extend(l2) ultimately execute the same code (the list_extend function in listobject.c). The only differences are: 1. += reassigns l1 (to itself for lists, but the reassignment supports immutable types that aren't the same object after), which makes it illegal if l1 is actually an attribute of an immutable object; for example, t = ([],), t[0] += lst would fail, while t[0].extend(lst) would work. 2. l1 += l2 uses dedicated bytecodes, while l1.extend(l2) uses generalized method dispatch; this makes += faster than extend.

2019年05月24日55分39秒

The fact that += must reassign l1 does mean that in some cases, the slower dispatch of extend is partially or wholly made up for by not assigning back to the left hand side. For example, if the list is an attribute of an object, self.l1 += l2 and self.l1.extend(l2) have identical performance on my Python 3.6 install, simply because real operation is more like self.l1 = self.l1.__iadd__(l2), which means it must perform a moderately expensive STORE_ATTR that self.l1.extend(l2) doesn't have to.

2019年05月24日55分39秒

Extending is ~4x faster on my machine than appending in a loop (16us vs 4us for 100 loops of zeros)

2019年05月24日55分39秒

extend() probably preallocates, while append() likely does not.

2019年05月24日55分39秒

You can't extend with just 6 since it's not iterable. And the second output in your example is wrong. 'abc' gets added as a single element since you passed it in to extend as a list with one element ['abc']: [1, 2, 3, 4, 5, 'abc']. To make your example output correct, change the abc line to: x.extend('abc'). And remove the x.extend(6) or change it to x.extend([6]).

2019年05月24日55分39秒

Does using the '+' to return extend have any affect on time complexity?

2019年05月24日55分39秒

franklin, see this answer for details: stackoverflow.com/a/28119966/2230844

2019年05月24日55分39秒

I don't see how this answers the question

2019年05月24日55分39秒

This answer does not contrast extend with append and therefore does not answer the question

2019年05月24日55分39秒

Why not =+ ?! More concise

2019年05月24日55分39秒

Both a.append(5, 6) and a.extend(5, 6) results in an error. For a.append(5, 6) it is "TypeError: append() takes exactly one argument (2 given)". I tried using both Python 2 and Python (Python 2.6.2 and Python 3.4.3 (with Anaconda 2.2.0)).

2019年05月24日55分39秒

Welcome to the SO. Answer itself is good (altough the formatting could use some refinement), however it doesn't add any value over already existing answers.

2019年05月24日55分39秒

stackoverflow.com/questions/19862116/… nasia jaffri

2019年05月24日55分39秒

This doesn't answer the question, which is about list methods and has nothing to do with this dictionary code

2019年05月24日55分39秒

Append has been explained using Dictionary .. it depends on individual how we see to try to set an example .. it could be list/dictionary :)

2019年05月24日55分39秒

Adding new answers to already long answered questions is kinda pointless unless you have something new to add.

2019年05月24日55分39秒

Okay :( sorry for this

2019年05月25日55分39秒

Why are you answering a question that has already been answer many times? This question is 10 years old...

2019年05月24日55分39秒