Given two lists:
x = [1,2,3]
y = [4,5,6]
What is the syntax to:
- Insert
x
intoy
such thaty
now looks like[1, 2, 3, [4, 5, 6]]
? - Insert all the items of
x
intoy
such thaty
now looks like[1, 2, 3, 4, 5, 6]
?
Given two lists:
x = [1,2,3]
y = [4,5,6]
What is the syntax to:
x
into y
such that y
now looks like [1, 2, 3, [4, 5, 6]]
?x
into y
such that y
now looks like [1, 2, 3, 4, 5, 6]
?
The question does not make clear what exactly you want to achieve.
List has the
append
method, which appends its argument to the list:There's also the
extend
method, which appends items from the list you pass as an argument:And of course, there's the
insert
method which acts similarly toappend
but allows you to specify the insertion point:To extend a list at a specific insertion point you can use list slicing (thanks, @florisla):
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:
You can also just do...
http://docs.python.org/tutorial/datastructures.html
Do you mean
append
?Or merge?
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
Or if you want to concatenate two list then you can use + sign