0

I have a function that returns two variables. However, sometimes I just want to get the output of one variable. What's the most shorthand/concise way to do that? If I just try to set one variable I get a 'Too many values to unpack error'

I tried just leaving the first or second field blank. Look at the code below. Get the same error as before.

Problem:

def function:
 return 1, 2

var1=function()

var1 == 1

Tried:

var1,=function()

I want a notation similar to:


var1,var2=function()

var1,=function()

so in this case var 1 should equal 1

mnsupreme
  • 125
  • 1
  • 6

2 Answers2

4

You can use _ (underscore), it is used like that for "garbage" values

var1, _ = function()

if you don't like that, you can also use indexing

var1 = function()[0]
0

This question has already been answered here, the best practice is the one given by Luke Woodward;

You can use x = func()[0] to return the first value, x = func()[1] to return the second, and so on. If you want to get multiple values at a time, use something like x,y = func()[2:4].

BramH
  • 221
  • 5
  • 24