Suppose I have a tuple in a list like this:
>>> t = [("asdf", )]
I know that the list always contains one 1-tuple. Currently I do this:
>>> dummy, = t
>>> value, = dummy
>>> value
'asdf'
Is there a shorter and more elegant way to do this?
Suppose I have a tuple in a list like this:
>>> t = [("asdf", )]
I know that the list always contains one 1-tuple. Currently I do this:
>>> dummy, = t
>>> value, = dummy
>>> value
'asdf'
Is there a shorter and more elegant way to do this?
Try
[(val, )] = t
I don't think there is a clean way to go about it.
val = t[0][0]
is my initial choice, but it looks kind of ugly.[(val, )] = t
also works but looks ugly too. I guess it depends on which is easier to read and what you want to look less ugly,val
ort
I liked Lior's idea that unpacking the list and tuple contains an assert.
Try
It's better than
t[0][0]
because it also asserts that your list contains exactly 1 tuple with 1 value in it.