Ignore Python Multiple Return Value

Ignore Python Multiple Return Value

We can ignore one or more return values according to the needs of the user in Python. In this blog, we will learn how we can ignore multiple return values with the help of examples.

Table of Contents:

Ignoring a Return Value in Python

Suppose there is a function that returns multiple values in Python, but we need just one value, then we can use underscore(“_”) to ignore the other returned values. Let’s understand this with an example:

Example to Ignore a returned value in Python:

Code:

def get_values():
    return 1, 2, 3

intellipaat1, intellipaat2, _ = get_values()
print(intellipaat1, intellipaat2)  

Output: 1,2

Explanation: Here we have ignored a returned value with the help of underscore.

Ignoring All Return Values in Python

We can also ignore all the returned values in Python, with the help of not storing it in any variables.

Example to Ignore all returned values in Python:

Code:

def get_values():
    return 1, 2, 3

get_values()

Output: Nothing

Explanation: Here we have returned the value but not stored it in any variable or array. So all the returned values are ignored here.

Ignoring Specific Return Values in Python

We can also ignore specific returned values in Python by using slicing. Let’s learn this with an example:

Example to Ignore specific returned value in Python:

Code:

def get_values():
    return 1, 2, 3, 4, 5

a, b = get_values()[:2]
print(a, b)  

Output: 1 2

Explanation: Here with the help of the slicing operator, we have just stored the first two returned values, and the rest are ignored.

Conclusion

So far in this article, we have learned how to ignore a single or multiple returned value in Python. If you want to learn more about Python Programming language, you must refer to our Python Course.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner