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.
# 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) |
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.
# 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) |
The original list is : [4, 5] The first argument is : 4 The second argument is : 5
Recommended Posts:
- Python | Split dictionary keys and values into separate lists
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Convert column to separate elements in list of lists
- Python | Custom list split
- Python | Split list into lists by particular value
- Python | Split a list having single integer
- Python | Split nested list into two lists
- Python | Split a list into sublists of given lengths
- Python | Split string into list of characters
- Python | Split list in uneven groups
- Python | Split given list and insert in excel file
- Python | Split dictionary of lists to list of dictionaries
- Python | Split strings in list with same prefix in all elements
- Python | Split list of strings into sublists based on length
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
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.



