232

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]?
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Soumya
  • 5,262
  • 8
  • 31
  • 30

6 Answers6

410

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] 
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
127

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]
Sergey
  • 11,892
  • 2
  • 41
  • 52
  • 48
    If you want to 'extend' to a specific insertion point, you can use list slicing syntax (see http://stackoverflow.com/a/7376026/1075152) – florisla Oct 18 '12 at 13:46
  • 10
    @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 Apr 12 '16 at 19:58
  • 2
    @weaver While it's the only solution to do *that* (extend at a specific index), that was not the original question. – florisla Sep 15 '16 at 08:30
  • @florisla That's pedantry. – weaver Sep 15 '16 at 15:55
35
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

Coding District
  • 11,901
  • 4
  • 26
  • 30
5

You can also just do...

x += y
user3707850
  • 73
  • 3
  • 8
  • 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. – HTNW Nov 19 '17 at 06:45
  • I do not have the reputation to do that, but if someone else can! – user3707850 Feb 08 '18 at 19:05
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]
squal
  • 185
  • 3
  • 14
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[:])

Raj Stha
  • 1,043
  • 11
  • 18