Home > Back-end >  Append 2 lists to a list
Append 2 lists to a list

Time:12-04

let's say I have 3 lists:

a=[1,2,3]
b=[4,5,6]
c=[]

What I wanna do is append these two lists so the result would look like this:

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

CodePudding user response:

You can get the result using c = [a, b]. Append method is useful especially when you are looping through a list of lists.

CodePudding user response:

Just append normally using .append():

c.append(a)
c.append(b)

You may as well create the list like so in the first place:

c = [a, b]

CodePudding user response:

i personally will use:

c = [a,b]

CodePudding user response:

Here's a one liner solution. You can use the extend() method instead. But since the extend method takes only one parameter, make sure to add both a and b as a tuple value.

a=[1,2,3]
b=[4,5,6]
c=[]

c.extend((a,b))
print(c)

Output:

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

CodePudding user response:

a = [1,2,3]
b = [3,4,5]
c = []
c.append(a)
c.append(b)
print(c)

This should work

  • Related