2

I have a 3x2 list called x and a 1x2 list called y:

x=[[1,2],[3,4],[5,6]]

and

y=[10,20]

my question is how to concatenate y to the end of x to end up with a 4x2 list like:

x=[[1,2],[3,4],[5,6],[10,20]]

I've tried this:

xx=[x,y]

but it gives me this which is not a 4x2 list:

xx=[[[1,2],[3,4],[5,6]],[10,20]]

5 Answers 5

9
>>> x = [[1, 2], [3, 4], [5, 6]]
>>> x
[[1, 2], [3, 4], [5, 6]]
>>> x.append([10, 20])
>>> x
[[1, 2], [3, 4], [5, 6], [10, 20]]

Or:

>>> x = [[1, 2], [3, 4], [5, 6]]
>>> x
[[1, 2], [3, 4], [5, 6]]
>>> x += [[10, 20]] # a list with a list as its only element
>>> x
[[1, 2], [3, 4], [5, 6], [10, 20]]
Sign up to request clarification or add additional context in comments.

Comments

9

Given:

x = [[1,2],[3,4],[5,6]]
y = [10,20]

this:

x.append(y)

will give you:

[[1, 2], [3, 4], [5, 6], [10, 20]]

Note however that this modifies x.

If you don't want to modify x, this is another way:

 xx = x + [y[:]]

setting xx to:

 [[1, 2], [3, 4], [5, 6], [10, 20]]

We use y[:] rather than simply y in the above assignment because we want to create separate copy of y for xx so later, should (the original) y be modified it would not lead to changes in xx.

4 Comments

Note that even in your second example you can indirectly modify xx by modifying y. (e.g. xx = x + [y]; y.append(3)).
@mgilson Ah .. good catch, thanks. I'll update my answer with either a cautionary note or a code fix shortly.
It does address my comment, although I would probably add a sentence to the answer saying why you used [y[:]] instead of just [y]. That way, the OP (or anyone else who comes across this) can have all the information necessary to pick which form is more appropriate for their application. My guess is that creating a shallow copy is probably unnecessary here, but the question is a little too vague to really know for sure.
:^) Well take your time then and enjoy your noodles!
5
>>> x=[[1,2],[3,4],[5,6]]
>>> y=[10,20]
>>> x.append(y)  # or x.append(list(y)) to append a shallow copy of y
>>> x
[[1, 2], [3, 4], [5, 6], [10, 20]]

2 Comments

@JonClements -- While you're (mostly) correct, it does have a slightly different meaning. Using list(y) means that changes to y won't show up in x. In other words, list(y) appends a (shallow) copy of y instead of the actual object.
@mgilson Yup - I'm aware of that. But then, since the OP wasn't massively clear, it's more likely a simple append is fine, and y can just go its merry way somewhere else. Ashwini has made a good comment and update here... so +1 from me for this one.
2

If you want a new list:

z = x + [y]

Note, that using [y] makes the content a list within a list, so that this works.

If you want to modify x inplace, then:

x.append(y)

Comments

1

Although I don't use it often myself, I think it's worth mentioning the list's extend member function:

>>> x=[[1,2],[3,4],[5,6]]
>>> y=[10,20]
>>> x.extend([y])
>>> x
[[1, 2], [3, 4], [5, 6], [10, 20]]

http://docs.python.org/tutorial/datastructures.html#more-on-lists

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.