首页 > 解决方案 > How do I make a new array from values and arrays, without the new array containing an array

问题描述

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?

标签: python

解决方案


您可以将xand转换z为列表,然后像这样将它们链接在一起;

a = [x] + y + [z]

或者在Python 3.5+中,您可以y在构建列表时解包,如下所示:

a = [x, *y, z]

推荐阅读