
Python Learning Notes
What Is the Difference Between append() and + in Python Lists?
A beginner-friendly Python note on append(), +, extend(), nested lists, and combining list values.
Python 学习笔记
append() 和 + 有什么区别?
一篇适合初学者的 Python 笔记,讲清楚 append()、+、extend()、嵌套列表和列表合并。
When beginners add items to a Python list, they often mix up append() and +. Both seem to “add something,” but they do different things, especially when the thing you add is another list.
This note focuses on one key difference: append() adds the whole object as one element; + combines two lists into a new list.
Common Mistake
Suppose we have this list:
items = [1, 2]
items.append([3, 4])
print(items)
Many students expect:
[1, 2, 3, 4]
But the actual output is:
[1, 2, [3, 4]]
The list [3, 4] was not unpacked. It was added as one whole element inside items.
Core Idea
append() means: add one element to the end of a list.
The key phrase is “one element.” Whether you pass in a number, a string, or another list, append() treats it as one element.
items = [1, 2]
items.append(3)
print(items)
Output:
[1, 2, 3]
This feels natural because 3 is a single element.
But if you write:
items = [1, 2]
items.append([3, 4])
print(items)
the output is:
[1, 2, [3, 4]]
because the list [3, 4] itself becomes one element.
Use + to Combine Two Lists
If you want [1, 2, 3, 4], use +:
items = [1, 2]
new_items = items + [3, 4]
print(new_items)
Output:
[1, 2, 3, 4]
Here, + connects two lists and creates a new list. It does not put [3, 4] inside as one element; it places 3 and 4 after the original items.
append() Changes the Original List
There is another important difference: append() changes the original list in place.
items = [1, 2]
items.append(3)
print(items)
Output:
[1, 2, 3]
The original items has changed.
But + usually creates a new list:
items = [1, 2]
new_items = items + [3]
print(items)
print(new_items)
Output:
[1, 2]
[1, 2, 3]
This difference will matter later when you learn about list mutability.
Comparison
| Code | Result | Good for |
|---|---|---|
items.append(3) |
[1, 2, 3] |
Adding one single element |
items.append([3, 4]) |
[1, 2, [3, 4]] |
Adding the whole list as one element |
items + [3, 4] |
[1, 2, 3, 4] |
Combining two lists |
extend() Can Add Items One by One
If you want to change the original list and add each item from another list, use extend():
items = [1, 2]
items.extend([3, 4])
print(items)
Output:
[1, 2, 3, 4]
A simple rule:
append()adds one thing.extend()adds items from another list one by one.+combines two lists and gives a new list.
Practice
- If
items = [1, 2], what isitemsafteritems.append([3])? - If
items = [1, 2], what is the result ofitems + [3]? - Challenge: without using
+, add[3, 4]item by item to[1, 2].
One-Sentence Takeaway
append() adds the whole object as one element; + combines two lists into a new list.
Next time you see an extra pair of brackets inside a list, check whether you used append([ ... ]).
给 list 添加内容时,很多同学会把 append() 和 + 混在一起用。它们看起来都能“加东西”,但实际效果不一样,尤其是当你想加入的是另一个 list 时。
这一篇只讲一个核心区别:append() 是把整个对象作为一个元素加进去;+ 是把两个 list 合成一个新的 list。
常见误解
假设现在有一个 list:
items = [1, 2]
items.append([3, 4])
print(items)
很多同学以为输出会是:
[1, 2, 3, 4]
但实际输出是:
[1, 2, [3, 4]]
注意最后的 [3, 4] 没有被拆开。它作为一个整体,被放进了 items 里面。
核心概念
append() 的意思是:在 list 末尾添加一个元素。
关键是“一个元素”。不管你放进去的是数字、字符串,还是另一个 list,append() 都会把它当成一个元素。
items = [1, 2]
items.append(3)
print(items)
输出:
[1, 2, 3]
这很符合直觉,因为 3 本来就是一个元素。
但如果你写:
items = [1, 2]
items.append([3, 4])
print(items)
输出就是:
[1, 2, [3, 4]]
因为 [3, 4] 这个 list 本身变成了一个元素。
用 + 合并两个 list
如果你想得到 [1, 2, 3, 4],可以用 +:
items = [1, 2]
new_items = items + [3, 4]
print(new_items)
输出:
[1, 2, 3, 4]
这里 + 的作用是把两个 list 连接起来,生成一个新的 list。它不是把 [3, 4] 作为一个元素塞进去,而是把里面的 3 和 4 接到后面。
append() 会改变原 list
还有一个很重要的区别:append() 会直接修改原来的 list。
items = [1, 2]
items.append(3)
print(items)
输出:
[1, 2, 3]
原来的 items 已经变了。
而 + 通常会产生一个新的 list:
items = [1, 2]
new_items = items + [3]
print(items)
print(new_items)
输出:
[1, 2]
[1, 2, 3]
这也是后面理解 list 可变性时很重要的一点。
对比一下
| 写法 | 输出结果 | 适合情况 |
|---|---|---|
items.append(3) |
[1, 2, 3] |
添加一个单独元素 |
items.append([3, 4]) |
[1, 2, [3, 4]] |
想把整个 list 当作一个元素 |
items + [3, 4] |
[1, 2, 3, 4] |
想合并两个 list |
extend() 也可以逐个加入
如果你想修改原 list,同时把另一个 list 里的元素逐个加进去,可以用 extend():
items = [1, 2]
items.extend([3, 4])
print(items)
输出:
[1, 2, 3, 4]
简单记:
append():加一个东西。extend():把另一个 list 里的东西逐个加进来。+:合并两个 list,得到新 list。
小练习
items = [1, 2],执行items.append([3])后,items是什么?items = [1, 2],items + [3]的结果是什么?- 挑战题:不用
+,把[3, 4]逐个加入[1, 2]。
一句话记住
append() 把整个对象当成一个元素加进去;+ 把两个 list 合并成一个新 list。
下次 list 里突然多了一层方括号,先检查是不是用了 append([ ... ])。
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us a private message.
有问题或建议?
如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。
Related Learning
Continue exploring related learning paths.
These related pages help students and parents move from interest to the right next step.