Python | Split and Pass list as separate parameter

With the advent of programming paradigms, there has been need to modify the way one codes. One such paradigm is OOPS. In this, we have a technique called modularity, which stands for making different modules/functions which perform independent tasks in program. In this, we need to pass more than just variable, but a list as well. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using tuple()
This task can be performed using the tuple(). In this, we convert the pair list to tuple and by this way we separate individual elements as variables, ready to be sent to function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Split and Pass list as separate parameter
# using tuple()
  
# Helper function for demonstration
def pass_args(arg1, arg2):
    print("The first argument is : " +  str(arg1))
    print("The second argument is : " +  str(arg2))
  
# initialize list
test_list = [4, 5]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Split and Pass list as separate parameter
# using tuple()
one, two = tuple(test_list)
pass_args(one, two)

chevron_right


Output :



The original list is : [4, 5]
The first argument is : 4
The second argument is : 5

 

Method #2 : Using * operator
Using * operator is the most recommended method to perform this task. The * operator unpacks the dual list into args and hence solving our problem.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Split and Pass list as separate parameter
# using * operator
  
# Helper function for demonstration
def pass_args(arg1, arg2):
    print("The first argument is : " +  str(arg1))
    print("The second argument is : " +  str(arg2))
  
# initialize list
test_list = [4, 5]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Split and Pass list as separate parameter
# using * operator
pass_args(*test_list)

chevron_right


Output :

The original list is : [4, 5]
The first argument is : 4
The second argument is : 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.