Reputation: 33
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
Upvotes: 3
Views: 1267
Reputation: 28656
You could do it with parameter unpacking.
>>> (lambda *a: a)(0, *x)
(0, 1, 2, 3, 4)
But don't.
Upvotes: 1
Reputation: 779
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)
Upvotes: 2
Reputation: 600059
You shouldn't use unpacking here, but just concatenation.
y = [0] + x
Upvotes: 2