Ignore python multiple return value Ignore python multiple return value python python

Ignore python multiple return value


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].


One common convention is to use a "_" as a variable name for the elements of the tuple you wish to ignore. For instance:

def f():    return 1, 2, 3_, _, x = f()


If you're using Python 3, you can you use the star before a variable (on the left side of an assignment) to have it be a list in unpacking.

# Example 1: a is 1 and b is [2, 3]a, *b = [1, 2, 3]# Example 2: a is 1, b is [2, 3], and c is 4a, *b, c = [1, 2, 3, 4]# Example 3: b is [1, 2] and c is 3*b, c = [1, 2, 3]       # Example 4: a is 1 and b is []a, *b = [1]


matomo