I am wanting to append two returned lists to two different lists such as
def func():
return [1, 2, 3], [4, 5, 6]
list1.append(), list2.append() = func()
Any ideas?
I am wanting to append two returned lists to two different lists such as
def func():
return [1, 2, 3], [4, 5, 6]
list1.append(), list2.append() = func()
Any ideas?
Why not just storing the return values?
With this, if
a
was previously[10]
andb
was previously[20]
, you'll have this result:Nah, that wasn't difficult, was it?
By the way, you probably want to merge the lists. For this, you can use
extend
:Hope it helps!
You'll have to capture the return values first, then append:
You appear to be returning lists here, are you certain you don't mean to use
list.extend()
instead?If you were extending
list1
andlist2
, you could use slice assignments:but this is a) surprising to newcomers and b) rather unreadable in my opinion. I'd still use the separate assignment, then extend calls:
A one line solution isn't possible (unless you use some cryptic hack, which is always a bad idea).
The best you can get is:
It's readable and efficient.