I have a tuple like this
t = (1, '0076', 'AU', '9927016803A', '9927013903B', '0010', 'VO')
and I want to extract the first 6 values as tuple (in order) and the last value as a string.
The following code already works, but I am wondering if there is a "one-liner" to achieve what I'm looking for.
# works, but it's not nice to unpack each value individually
cid,sc,ma,comp,mat,step,alt = t
t_new = (cid,sc,ma,comp,mat,step,)
print(t_new, alt) # (1, '0076', 'AU', '9927016803A', '9927013903B', '0010') VO
This is very close to what I'm looking for, but it returns the first values as a list instead of a tuple:
# works, but returns list
*t_new,alt = t
print(t_new, alt) # [1, '0076', 'AU', '9927016803A', '9927013903B', '0010'] VO
I've already tried the following, but w/o success:
tuple(*t_new),alt = t # SyntaxError
(*t_new),alt = t # still a list
(*t_new,),alt = t # ValueError
If there's no other way, I will probably go with my second attempt and cast the list to a tuple.
why not just:
t = (1, '0076', 'AU', '9927016803A', '9927013903B', '0010', 'VO')
t_new, alt = t[:-1], t[-1]
print(t_new, alt) # (1, '0076', 'AU', '9927016803A', '9927013903B', '0010') VO
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