Python | Unpack whole list into variables

Sometimes, while working with list, we can have a 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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


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.