Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Unpack whole list into variables

Python | Unpack whole list into variables

Sometimes, while working with list, we can wish to have each of element of list to be converted or unpacked into required variables. This problem can occur in web development domain. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using “=” operator This task can be performed using a “=” operator. In this we just ensure enough variables as the count of list elements and assign them to list, the list elements get allotted to variables in order of their assignment. 

Python3




# Python3 code to demonstrate working of
# Unpack whole list into variables
# using "=" operator
 
# initialize list
test_list = [1, 3, 7, 4, 2]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpack whole list into variables
# using "=" operator
one, two, three, four, five = test_list
 
# printing result
print("Variables as assigned are : " + str(one) + " "
                                     + str(two) + " "
                                     + str(three) + " "
                                     + str(four) + " "
                                     + str(five))


Output : 

The original list is : [1, 3, 7, 4, 2]
Variables as assigned are : 1 3 7 4 2

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

  Method #2 : Using Namedtuple This problem can be solved using a namedtuple, which can store the list elements with a variable name that can be accessed by using a dot operator succeeding with name of variable initialized. 

Python3




# Python3 code to demonstrate working of
# Unpack whole list into variables
# using Namedtuple
from collections import namedtuple
 
# initialize list
test_list = [1, 3, 7, 4, 2]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpack whole list into variables
# using Namedtuple
temp = namedtuple("temp", "one two three four five")
res = temp(*test_list)
 
# printing result
print("Variables as assigned are : " + str(res.one) + " "
                                     + str(res.two) + " "
                                     + str(res.three) + " "
                                     + str(res.four) + " "
                                     + str(res.five))


Output : 

The original list is : [1, 3, 7, 4, 2]
Variables as assigned are : 1 3 7 4 2

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

 Method #3 : Using numpy:

Algorithm:

  1. Create a list named “test_list” and initialize it with some values.
  2. Print the original list.
  3. Use reduce function from functools module with a lambda function that takes two arguments x and y, and
  4. returns a numpy array with the elements of x and y using np.array([*x, y]).
  5. Pass the test_list to reduce() function with the initial value of an empty numpy array np.array([]).
  6. Unpack the whole list into variables res by accessing the array elements with their respective index numbers.
  7. Print the variables.

Python3




import numpy as np
from functools import reduce
 
# initialize list
test_list = [1, 3, 7, 4, 2]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpack whole list into variables
# using NumPy and reduce
res = reduce(lambda x, y: np.array([*x, y]), test_list, np.array([]))
 
# printing result
print("Variables as assigned are : " + str(res[0]) + " "
+ str(res[1]) + " "
+ str(res[2]) + " "
+ str(res[3]) + " "
+ str(res[4]))
#This code is contributed by Jyothi pinjala


Output:
The original list is : [1, 3, 7, 4, 2]
Variables as assigned are : 1 3 7 4 2

Time Complexity:

The time complexity of this code is O(n), where n is the number of elements in the test_list. The reduce function iterates over each element of the list exactly once.

Auxiliary Space:

The space complexity of this code is O(n), where n is the number of elements in the test_list. The reduce function creates a new numpy array for each element in the list, resulting in the total space complexity of O(n).

Method #3 : Using list comprehension:

  1. Initialize the original list, test_list.
  2. Print the original list.
  3. Use a list comprehension to create a new list that contains all the elements of test_list.
  4. Unpack the new list into individual variables named one, two, three, four, and five.
  5. Print the values of the variables.

Python3




# initialize list
test_list = [1, 3, 7, 4, 2]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpack whole list into variables using list comprehension
one, two, three, four, five = [test_list[i] for i in range(len(test_list))]
 
# printing result
print("Variables as assigned are : " + str(one) + " "
                                    + str(two) + " "
                                    + str(three) + " "
                                    + str(four) + " "
                                    + str(five))
#This code is contributed by Rayudu.


Output

The original list is : [1, 3, 7, 4, 2]
Variables as assigned are : 1 3 7 4 2

Time complexity:  O(N) time complexity because it iterates over the elements of test_list exactly once to create the new list. The variable assignment statements and the print statements each run in constant time, so the overall time complexity of the algorithm is O(N).

Auxiliary Space: O(N) because it creates a new list that is the same size as the original list. The variables one, two, three, four, and five each take up constant space, so they do not contribute to the space complexity. The print statements also use constant space, so the overall space complexity of the algorithm is O(N).

RELATED ARTICLES

Most Popular

Recent Comments