240

Given two lists:

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

What is the syntax to:

  1. Insert x into y such that y now looks like [1, 2, 3, [4, 5, 6]]?
  2. Insert all the items of x into y such that y now looks like [1, 2, 3, 4, 5, 6]?
3
  • reverse listB, thence [listA.insert(pos,x) for x in [listB]] Commented Jun 14, 2011 at 2:31
  • see also this post: stackoverflow.com/questions/5805892/… Commented Sep 2, 2014 at 7:26
  • 1
    @JohnMee Do you mean listA.insert(pos, x) for x in listB[::-1]? Commented Jun 21, 2017 at 6:24

6 Answers 6

418

Do you mean append?

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

Or merge?

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> x + y
[1, 2, 3, 4, 5, 6]
>>> x.extend(y)
>>> x
[1, 2, 3, 4, 5, 6] 
Sign up to request clarification or add additional context in comments.

2 Comments

Is that in place or yields a new instance?
x.extend(y) is in place, x+y is returning new list. And x += y, which was not mentioned here, is similar to the extend.
141

The question does not make clear what exactly you want to achieve.

List has the append method, which appends its argument to the list:

>>> list_one = [1,2,3]
>>> list_two = [4,5,6]
>>> list_one.append(list_two)
>>> list_one
[1, 2, 3, [4, 5, 6]]

There's also the extend method, which appends items from the list you pass as an argument:

>>> list_one = [1,2,3]
>>> list_two = [4,5,6]
>>> list_one.extend(list_two)
>>> list_one
[1, 2, 3, 4, 5, 6]

And of course, there's the insert method which acts similarly to append but allows you to specify the insertion point:

>>> list_one.insert(2, list_two)
>>> list_one
[1, 2, [4, 5, 6], 3, 4, 5, 6]

To extend a list at a specific insertion point you can use list slicing (thanks, @florisla):

>>> l = [1, 2, 3, 4, 5]
>>> l[2:2] = ['a', 'b', 'c']
>>> l
[1, 2, 'a', 'b', 'c', 3, 4, 5]

List slicing is quite flexible as it allows to replace a range of entries in a list with a range of entries from another list:

>>> l = [1, 2, 3, 4, 5]
>>> l[2:4] = ['a', 'b', 'c'][1:3]
>>> l
[1, 2, 'b', 'c', 5]

4 Comments

If you want to 'extend' to a specific insertion point, you can use list slicing syntax (see stackoverflow.com/a/7376026/1075152)
@florisla's comment should be the accepted answer. It's the only way to insert a list into another list in place at an arbitrary location (not just at the end).
@weaver While it's the only solution to do that (extend at a specific index), that was not the original question.
@florisla That's pedantry.
36
foo = [1, 2, 3]
bar = [4, 5, 6]

foo.append(bar) --> [1, 2, 3, [4, 5, 6]]
foo.extend(bar) --> [1, 2, 3, 4, 5, 6]

http://docs.python.org/tutorial/datastructures.html

2 Comments

how do you insert bar after 2 and keep it flatten? [1,2,4,5,6,3]
I've found that this: foo[2:2]=bar generates a flattened list. I learned that because it was not what I wanted.
6

You can also just do...

x += y

2 Comments

This should be a comment on the accepted answer, because that one mentions x + y and x += y is just the same thing but in place.
I do not have the reputation to do that, but if someone else can!
4

If you want to add the elements in a list (list2) to the end of other list (list), then you can use the list extend method

list = [1, 2, 3]
list2 = [4, 5, 6]
list.extend(list2)
print list
[1, 2, 3, 4, 5, 6]

Or if you want to concatenate two list then you can use + sign

list3 = list + list2
print list3
[1, 2, 3, 4, 5, 6]

Comments

1

If we just do x.append(y), y gets referenced into x such that any changes made to y will affect appended x as well. So if we need to insert only elements, we should do following:

x = [1,2,3] y = [4,5,6] x.append(y[:])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.