0

I have these variables:

x = 1
y = [2, 3, 4]
z = 5

I want to add them all to a new array (something like this):

a = [x, y, z]

Now a is [1, [2, 3, 4], 5]

However, I want a to be [1, 2, 3, 4, 5]

What's the most concise way to accomplish that?

Ben Rubin
  • 6,909
  • 7
  • 35
  • 82

1 Answers1

3

You can convert x and z into lists and then chain them all together like this;

a = [x] + y + [z]

Or in Python 3.5+, you can unpack y as you build the list, like this:

a = [x, *y, z]
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45