Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Assign multiple variables with list values

Python | Assign multiple variables with list values

We generally come through the task of getting certain index values and assigning variables out of them. The general approach we follow is to extract each list element by its index and then assign it to variables. This approach requires more line of code. Let’s discuss certain ways to do this task in compact manner to improve readability. 

Method #1 : Using list comprehension By using list comprehension one can achieve this task with ease and in one line. We run a loop for specific indices in RHS and assign them to the required variables. 

Python3




# Python3 code to demonstrate
# to assign variables from list element
# using list comprehension
 
# initializing list 
test_list = [1, 4, 5, 6, 7, 3]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list comprehension
# to assign variables from list element
var1, var2, var3 = [test_list[i] for i in (1, 3, 5)]
 
# printing result
print ("The variables are : " +  str(var1) +
                           " " + str(var2) +
                            " " + str(var3))


Output:

The original list is : [1, 4, 5, 6, 7, 3]
The variables are : 4 6 3

Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using additional space res other than the input list itself with the same size of input list.

  Method #2 : Using itemgetter() itemgetter function can also be used to perform this particular task. This function accepts the index values and the container it is working on and assigns to the variables. 
 

Python3




# Python3 code to demonstrate
# to assign variables from list element
# using itemgetter()
from operator import itemgetter
 
# initializing list 
test_list = [1, 4, 5, 6, 7, 3]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using  itemgetter()
# to assign variables from list element
var1, var2, var3 = itemgetter(1, 3, 5)(test_list)
 
# printing result
print ("The variables are : " +  str(var1) +
                           " " + str(var2) +
                            " " + str(var3))


Output:

The original list is : [1, 4, 5, 6, 7, 3]
The variables are : 4 6 3

  Method #3 : Using itertools.compress() compress function accepts boolean values corresponding to each index as True if it has to be assigned to the variable and False it is not to be used in the variable assignment. 

Python3




# Python3 code to demonstrate
# to assign variables from list element
# using itertools.compress()
from itertools import compress
 
# initializing list 
test_list = [1, 4, 5, 6, 7, 3]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using  itertools.compress()
# to assign variables from list element
var1, var2, var3 = compress(test_list, (0, 1, 0, 1, 0, 1, 0))
 
# printing result
print ("The variables are : " +  str(var1) +
                           " " + str(var2) +
                            " " + str(var3))


Output:

The original list is : [1, 4, 5, 6, 7, 3]
The variables are : 4 6 3

Method #4:  Using dictionary unpacking

Approach

using dictionary unpacking. We can create a dictionary with keys corresponding to the variables and values corresponding to the indices we want, and then unpack the dictionary using dictionary unpacking.

Algorithm

1. Create a list with the given values.
2. Create a dictionary with keys corresponding to the variables and values corresponding to the indices we want.
3. Unpack the dictionary using dictionary unpacking.

Python3




lst = [1, 4, 5, 6, 7, 3]
var_dict = {'var1': lst[1], 'var2': lst[3], 'var3': lst[5]}
var1, var2, var3 = var_dict.values()
print(var1, var2, var3)


Output

4 6 3

Time Complexity: O(1)
Space Complexity: O(k), where k is the number of variables

RELATED ARTICLES

Most Popular

Recent Comments