Swapnil1456
Swapnil1456

Reputation: 45

Packing values back in tuple in python

I am working on a project where I got some data back in the form of a tuple, since a lot of it wasn't really needed I unpacked it and took what I needed, I need to make a new tuple with it how can I pack it back to original state within the tuple again ?

example code is :

name,*unused,amount=(name,age,address,email,contact,amount)
#this is how data gets unpacked
  name=name
  amount=amount
  unused=['age','address','email','contact']

when I try packing it again using the tuple itself it becomes

data=(name,['age','address','email','contact'],amount)

is there any way to pack it back as the original state, as of now I'm manually unpacking the list and making the tuple again and want to avoid use of this extra function, looking for an internal way of doing it .

Upvotes: 0

Views: 321

Answers (1)

user459872
user459872

Reputation: 24592

If I understand your question correctly, you are looking for this?. You can use * operator to unpack the the list elements

>>> first, *middle, last = (1, 2, 3, 4, 5)
>>> first
1
>>> middle
[2, 3, 4]
>>> last
5
>>> (first, *middle, last)
(1, 2, 3, 4, 5)

Upvotes: 2

Related Questions