Unpacking arguments in Python
Last Updated :
05 Sep, 2020
If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python.
Unpacking without storing the values: You might encounter a situation where you might not need all the values from a tuple but you want to store only some of them. In that case, you can use an _ to ignore certain values. Now, let's combine it with the above * implementation
Example 1:
Python3
# unpacking python tuple using _
# first and last value will be ignored
# and won't be stored second will be
# assigned to b and remaining will be
# assigned to x
_, b, *x, _ = ("I ", "love ", "Geeks ",
"for ", "Geeks ", 3000)
# print details
print(b)
print(x)
Output:
love
['Geeks ', 'for ', 'Geeks ']
Explanation:
Notice here the first and last variables are set to underscore ( _ ). In python, underscore is used for ignoring values or throw away variables, which means that "I " and 3000 won't be stored.
Example 2:
Python3
# unpacking python tuple using _*
# first second and last value will be stored
# remaining will be ignored by using *_
a, b, *_, c = ("I ", "love ", "Geeks ",
"for ", "Geeks ", 3000)
# print details
print(a)
print(b)
print(c)
Output:
I
love
3000
Explanation:
Notice here in a and b, the first and second values get assigned and in c, the last value gets assigned. So, what about the third, fourth, and fifth? Well, they simply get ignored because we have used *_. If we used * with a variable then all those would get into that variable as a list. Since, we have used _ instead of a variable, so the entire list of words gets ignored completely.
Example 3: Well, the idea here is to create a function that will take in a list of numbers and return its sum, average, maximum, and minimum in the list. We will then reuse the function to get that we need for different use cases.
Python3
def arithmetic_operations(arr: list):
MAX = max(arr)
MIN = min(arr)
SUM = sum(arr)
AVG = SUM/len(arr)
return (SUM, AVG, MAX, MIN)
if __name__ == '__main__':
arr = [5, 8, 9, 12, 50, 3, 1]
# for all data
sum_arr, avg_arr, max_arr, min_arr = arithmetic_operations(arr)
print("CASE 1 ", sum_arr, avg_arr, max_arr, min_arr)
#for only avg and max
_, avg_arr, max_arr, _ = arithmetic_operations(arr)
print("CASE 2 ", avg_arr, max_arr)
# for only sum and min
sum_arr, *_, min_arr = arithmetic_operations(arr)
print("CASE 3 ", sum_arr, min_arr)
Output:
CASE 1 88 12.571428571428571 50 1
CASE 2 12.571428571428571 50
CASE 3 88 1
The above code is for demonstrating how you can have one function returning multiple values but use only the ones necessary at a time without wasting memory.
Similar Reads
Packing and Unpacking Arguments in Python Python provides the concept of packing and unpacking arguments, which allows us to handle variable-length arguments efficiently. This feature is useful when we donât know beforehand how many arguments will be passed to a function.Packing ArgumentsPacking allows multiple values to be combined into a
3 min read
Unpacking a Tuple in Python Tuple unpacking is a powerful feature in Python that allows you to assign the values of a tuple to multiple variables in a single line. This technique makes your code more readable and efficient. In other words, It is a process where we extract values from a tuple and assign them to variables in a s
2 min read
Types of Arguments in Python Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma. There are many types of arguments in Python .In this example, we will create a simple function in Python to check whether the number passed as an argument to the
3 min read
Python function arguments In Python, function arguments are the inputs we provide to a function when we call it. Using arguments makes our functions flexible and reusable, allowing them to handle different inputs without altering the code itself. Python offers several ways to use arguments, each designed for specific scenari
3 min read
Default arguments in Python Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.Default Arguments: Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argum
7 min read
Variable Length Argument in Python In this article, we will cover about Variable Length Arguments in Python. Variable-length arguments refer to a feature that allows a function to accept a variable number of arguments in Python. It is also known as the argument that can also accept an unlimited amount of data as input inside the func
4 min read
Tuple as function arguments in Python Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read
Command Line Arguments in Python The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are: Table of ContentUsing sys.argvUsing getopt moduleUsing a
5 min read
Python | os.unlink() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f
3 min read
Passing function as an argument in Python In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, decorators and lambda expressions. By passing a function as an argument, we can modify a functionâs behavior dynamically
5 min read