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.
# 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)) |
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.
# 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)) |
The original list is : [1, 3, 7, 4, 2] Variables as assigned are : 1 3 7 4 2
Recommended Posts:
- Python | Assign multiple variables with list values
- Python Variables
- Private Variables in Python
- Python | Extract key-value of dictionary in variables
- Global and Local Variables in Python
- Class or Static Variables in Python
- How to assign values to variables in Python and other languages
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Inserting variables to database table using Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Python | Difference between Pandas.copy() and copying through variables
- Python program to find number of local variables in a function
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
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.



