Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpacking values from list in Python 2.7

Python Newbie here: Given a list and a value, how can I unpack the list into separate objects and prepend a value. Length of the array isn't known, unlike in code below:

x = [1,2,3,4]
y = 0,x
y

Current Output:

(0, [1, 2, 3, 4])

Desired Output:

(0, 1, 2, 3, 4)

I know it can be done easily in Python 3 using y = 0,*x but how can it be done in Python 2.7?

Thanks

like image 259
Berneng Avatar asked Feb 04 '26 16:02

Berneng


2 Answers

You shouldn't use unpacking here, but just concatenation.

y = [0] + x
like image 162
Daniel Roseman Avatar answered Feb 07 '26 05:02

Daniel Roseman


Insert y into x and make x Tuple

x = [1,2,3,4]
y = 0
x.insert(0,y)
print(tuple(x))

output

(0, 1, 2, 3, 4)
like image 27
sachin dubey Avatar answered Feb 07 '26 06:02

sachin dubey