Here’s a note in Python list merging.
Environment
- Python 3.4.3
- Ubuntu 15.04
Bang or Non-Bang
In Python, we can merge 2 list in 2 way. extend
method and +
operator. But they effect in different way.
1 2 3 4 5 6 7 8 9 |
l = [1, 2, 3] r = l.extend([4, 5]) # l => [1, 2, 3, 4, 5] # r => None l = [1, 2, 3] r = l + [4, 5] # l => [1, 2, 3] # r => [1, 2, 3, 4, 5] |
extend
method- Extends the list.
- Returns
None
.
+
operator- Creates a new list from 2 list.
- Returns a new merged list.
- The original lists are not changed.
Thus, extend
method is a bang method, you can fall into a bag spiral if you use it by mistake.