I have a bunch of lists I want to append to a single list that is sort of the “main” list in a program I’m trying to write. Is there a way to do this in one line of code rather than like 10? I’m a beginner so I have no idea…
For a better picture of my question, what if I had these lists:
JavaScript
x
4
1
x = [1, 2, 3]
2
y = [4, 5, 6]
3
z = [7, 8, 9]
4
And want to append y and z to x. Instead of doing:
JavaScript
1
3
1
x.append(y)
2
x.append(z)
3
Is there a way to do this in one line of code? I already tried:
JavaScript
1
2
1
x.append(y, z)
2
And it wont work.
Advertisement
Answer
JavaScript
1
2
1
x.extend(y+z)
2
should do what you want
or
JavaScript
1
2
1
x += y+z
2
or even
JavaScript
1
2
1
x = x+y+z
2