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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.