Open In App

Unpacking arguments in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 05 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads