Given two lists:
x = [1,2,3]
y = [4,5,6]
What is the syntax to:
- Insert
xintoysuch thatynow looks like[1, 2, 3, [4, 5, 6]]? - Insert all the items of
xintoysuch thatynow 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]?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]
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[:])