0

I have multiple lists like below

x = []
a = [1, 2, 3]
b = [4, 5, 5]

I am trying to append a and b to x and get a result like below

print(x)
#[[1, 2, 3], [4, 5, 6]]

But I have no idea how to do this task. Append or other things just makes it like [1, 2, 3, 4, 5, 6] which i don't want to. Is there any methods in python for that or should I use some other packages?

  • Does this answer your question? [Take the content of a list and append it to another list](https://stackoverflow.com/questions/8177079/take-the-content-of-a-list-and-append-it-to-another-list) – sahasrara62 Jul 28 '22 at 04:44

5 Answers5

0

I just found out there is a method called insert for that... Sorry i am a newbie. Problem solved

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 29 '22 at 13:16
0

You can use append method try this:

l = []
a = [1, 2, 3]
b = [4, 5, 5]

l.append(a)
print(l)
l.append(b)
print(l)

Output:

[[1, 2, 3]]
[[1, 2, 3], [4, 5, 5]]
Hiral Talsaniya
  • 376
  • 1
  • 5
0

You can simply use append to insert any object in list including list. list is an object so you can simply do that an it'll work

>>> x = []
>>> a = [1, 2, 3]
>>> b = [4, 5, 5]
>>> x.append(a)
>>> x
[[1, 2, 3]]
>>> x.append(b)
>>> x
[[1, 2, 3], [4, 5, 5]]

Pranjal Doshi
  • 862
  • 11
  • 29
0

use from this code:

x = [a, b]
Alireza75
  • 513
  • 1
  • 4
  • 19
0

You can use:

x = []
a = [1, 2, 3]
b = [4, 5, 5]

x += [a, b]
print(x)

Output:

[[1, 2, 3], [4, 5, 5]]
René
  • 4,594
  • 5
  • 23
  • 52