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
You shouldn't use unpacking here, but just concatenation.
y = [0] + x
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With