How can i return two different values into separate variables? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i return two different values into separate variables?

def a(list1, list2): temp = list1 list2.extend(list1) list1.extend(temp) return list1, list2 abd=[1, 2, 3] dgf=[4, 5, 6] x,y =a(abd, dgf) #both list1 and list2 are type: list #I want to unpack list into x and list2 into y using "return" , please help

5th Apr 2018, 4:23 PM
Abdul akeem
Abdul akeem - avatar
5 Answers
+ 2
def is a reserved word as in x,y (abd,def) abd and other list needs to be initialised or use list ('abd') list.extend used incorrectly abd=[1,2,3] dfg=[3,4,5] def a(list1, list2): temp = list1[:] list1.extend(list2) list2.extend(temp) return list1, list2 x,y =a(abd, dfg) print (x) print (y)
5th Apr 2018, 5:02 PM
Louis
Louis - avatar
+ 2
You can return 2 because the comma makes it a tuple def a(list1, list2): temp = list1[:] list1.extend(list2) list2.extend(temp) return list1, list2 x,y =a(list ('abc'), list ('dfg')) print(x) print(y) https://code.sololearn.com/c2IEVbhMWiMD/?ref=app
5th Apr 2018, 5:43 PM
Louis
Louis - avatar
+ 1
python short answer... you can't. you can return a tuple/list/dictionary... and break it up.
5th Apr 2018, 5:24 PM
Amir Galanty
Amir Galanty - avatar
+ 1
Amir Galanty Smoliar The question has two parts. How can i return two different values into separate variables? The end state is 2 separate varibles that get their values from one function, and that can be done. The tuple is just the packaging that the 2 variables are kept in. By doing x,y=func(a,b) the tuple is unpacked into x and y. So you can return two different values into separate variables.
6th Apr 2018, 7:06 AM
Louis
Louis - avatar
0
Louis so it's not two... It's one tuple.
6th Apr 2018, 6:42 AM
Amir Galanty
Amir Galanty - avatar