Process tuple contents on the fly
Peter Otten
__peter__ at web.de
Mon Apr 15 15:07:32 EDT 2013
Tim Chase wrote:
> On 2013-04-15 11:25, Gnarlodious wrote:
>> Say I have a tuple I want to expand assigning to variables:
>>
>> tup = *func()
>> var = tup[0]
>> lst.append(tup[1])
>>
>> Or could I do it in one line?
>>
>> var, lst.append() = *func()
>>
>> So I want to append one variable to a list on the fly, is it
>> possible?
>
> I stumbled across this atrocity[*], which if you chose to use it,
> you'd deserve a kick in the pants:
>
> lst.append("Value I don't care about and will overwrite")
> var, lst[-1] = *func()
>
> It's not quite one step, but at least the *assignment* is one step :-)
I think the star is on the wrong side.
So:
>>> items = ["a", "b", "c"]
>>> def f(result=(4, 5, 6)): return result
...
>>> var, *items[len(items):] = f()
>>> var
4
>>> items
['a', 'b', 'c', 5, 6]
More information about the Python-list
mailing list