Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Split and Pass list as separate parameter

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




# 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)


Output : 

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

Time Complexity: O(n)
Auxiliary Space: O(n)

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




# 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)


Output : 

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

Time complexity: O(1) – The code has a constant number of operations and does not depend on the size of the input.
Auxiliary space: O(1) – The space used by the code remains constant, regardless of the size of the input.

Method #3: Using Indexing

We can directly access the elements of the list using their indices.

Python3




# Python3 code to demonstrate working of
# Split and Pass list as separate parameter
# using indexing
 
# 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 indexing
pass_args(test_list[0], test_list[1])


Output

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

Time complexity: O(1)
Auxiliary space: O(1) 

Method 4: using unpacking operator (**)

This method creates a dictionary with the argument names as keys (“arg1”, “arg2”, etc.) and the list values as values, and then passes that dictionary as separate keyword arguments to the function using the ** unpacking operator.

Python3




# Python3 code to demonstrate working of
# Split and Pass list as separate parameter
# using unpacking 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 unpacking operator (**)
pass_args(**{f"arg{i+1}": v for i,v in enumerate(test_list)})


Output

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

Time complexity: O(n), where n is the length of the list. This is because we need to iterate over the entire list to create the dictionary.

Auxiliary space complexity: O(n), where n is the length of the list. This is because we need to create a dictionary with n key-value pairs.

RELATED ARTICLES

Most Popular

Recent Comments