Skip to content
Related Articles

Related Articles

Python | Assign multiple variables with list values

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 21 Jun, 2022

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

  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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!