# Appending a nested sequence to a nested sequence as a nested sequence

I’d like to append a nested sequence to the last element of a list.

listL=[[1, 2] , [2, 3]]
listL[-1].append([4, 5])

I assume the above code should produce [[1, 2], [[2, 3], [4, 5]]], appending [4, 5] to [2, 3]. However, it produces [[1, 2][2, 3, [4, 5]], apparently appending to 3, not [2, 3].

How do I produce [[1, 2], [[2, 3], [4, 5]]]?

`listL = [[1, 2], [2, 3]]` is a list with two element. The first element is a list `[1, 2]`, the second element is the list `[2, 3]`. `listL[-1]` refers to the last element in the list, which is `[2, 3]`.

`list.append(self, object)` will add `object` to the list `self`. So `listL[-1].append([4, 5])` will append the entire object `[4, 5]` (which happens to be a list, but it could have been anything) to the list `[2, 3]`, resulting in `[2, 3, [4, 5]]` and the result you see.

Perhaps you are looking for `list.extend(self, iterable)` which will append each element in the given iterable to `self`. `listL[-1].extend([4, 5])` produces `[[1, 2], [2, 3, 4, 5]]`

Or, if you want three lists of two numbers each, `listL.append([4, 5])` would produce `[[1, 2], [2, 3], [4, 5]]`

You’re thinking about “appending to” incorrectly. `list.append` appends
an object (any object) to the end of the list.

`listL[-1]` is the list `[2,3]`. The new object is `[4,5]`. You’re
appending that to the `[2,3]` list, which makes `[2,3,[4,5]]`.

If you wanted a list `[4,5]` after the list `[2,3]` then you want to
be appending to `listL`, not to `listL[-1]`:

`````` listL.append([4,5])
``````

No; that would be making a new list that has `[2, 3]` inside of it as an element, and then appending `[4, 5]` to that list.

Yes; that is because `[4, 5]` was appended to the existing `[2, 3]` list. Before, there was a list with two elements in it: `2`, and `3`. If we append something to that list, it will have three elements: `2`, `3`, and whatever was appended (in this case, `[4, 5]`). So that result looks like: `[2, 3, [4, 5]]`.

No, I want neither [[1, 2], [2, 3, 4, 5]] nor [[1, 2], [2, 3], [4, 5]]. I want [[1, 2], [[2, 3], [4, 5]]]. How do I achieve that?

My thinking is listL[-1] refers to the last element of the list, which I incorrectly assume is [2, 3] with 3 being the last element of the last element. I want to append to [2, 3] to produce [[2, 3], [4, 5]].

You will need to nest your list `[2, 3]` inside another list: `[[2, 3]]`.

To “see” it, mentally substitute `x = [2, 3]` and `y = [4, 5]` in this:

``````items = [x]
items.append(y)
assert items == [x, y]
``````

As @flyinghyrax said, you will need to nest `[2, 3]` inside another list. You are not simply appending, you are replacing that element with a new object, which is a list containing the list `[2, 3]`. And then you are appending to that list.

e.g.

``````listL = [[1, 2], [2, 3]]
listL[-1] = [listL[-1]]  # now we have [[1, 2], [[2, 3]]]
listL[-1].append([4, 5])  # now it's [[1, 2], [[2, 3], [4, 5]]]
``````

I’ll just add that if you want to keep going (continue replacing the last element with a list, and appending to that), you probably want to sit down and write out an algorithm for how to keep track of you current depth, and recover that last element or keep a reference to it. After a few levels of this it’s gonna be messy.

It is not possible to do that, because that is not what “appending” means.

One way of doing it:

``````>>> data = [[1, 2], [3, 4], [5, 6]]
>>> data.append([data.pop(), [7, 8]])
>>> data
[[1, 2], [3, 4], [[5, 6], [7, 8]]]
``````