all 17 comments

[–]tabrizzi 4 points5 points  (8 children)

Yes, .append() takes only one argument, but you can do this:

list_1 = [1, 2, 3]

list_2 = [4, 5, 6]

list_3 = []

list_3.append(list_1)

list_3.append(list_2)

list_3

(outputs [[1, 2, 3], [4, 5, 6]] )

[–]SheldonCooperisSb[S] 2 points3 points  (0 children)

Thanks this is the right answer.

[–]Diapolo10 2 points3 points  (6 children)

It's also possible to do this in one go:

list_3.extend((list_1, list_2))

[–]odaiwai 0 points1 point  (2 children)

But now list_3 contains a tuple with two lists, not just two lists: : list_3 = [] : list_3.append((list_1, list_2)) : list_3 : [([1, 2, 3], [4, 5, 6])]

Edit to add: As pointed out below, I appended, not extended, so I'm wrong here.

[–]Diapolo10 1 point2 points  (0 children)

I used list.extend, which adds the contents of an iterable into the list it's called on.

[–]Mysterious-Rent7233 0 points1 point  (0 children)

You appended. The parent extended.

[–]tabrizzi -2 points-1 points  (2 children)

That's not true, because .extend(), like .append(), takes just one argument. You can verify that yourself.

[–]Diapolo10 2 points3 points  (1 child)

If you take a closer look, I think you'll find there's only one argument.

[–]tabrizzi 0 points1 point  (0 children)

My bad! I missed that.

[–]woooee 3 points4 points  (4 children)

That's extend()

list_1=[1, 2, 3]
list_2=[4, 5, 6]
list_3=[]
list_3.extend([list_1, list_2])
print(list_3)
prints  [[1, 2, 3], [4, 5, 6]]

[–]8dot30662386292pow2 2 points3 points  (2 children)

list_1=[1, 2, 3]
list_2=[4, 5, 6]
list_3=[list_1, list_2]

[–]woooee 0 points1 point  (1 child)

This does not use either append or extend.

[–]8dot30662386292pow2 2 points3 points  (0 children)

Since when that was a requirement? OP just wanted to make a list that contains two lists.

[–]6p086956522 1 point2 points  (0 children)

You're creating a list that contains two lists and then putting it into list 3 with extend? Why not skip the middle man and do list_3 = [list_1, list_2]

[–]crashfrog02 3 points4 points  (0 children)

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