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
5 ответов
+ 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)
+ 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
+ 1
python
short answer... you can't.
you can return a tuple/list/dictionary... and break it up.
+ 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.
0
Louis so it's not two...
It's one tuple.