Python | Assign multiple variables with list values - GeeksforGeeks

02 Jan, 2019
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 comprehensionBy 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 code to demonstrate # to assign variables from list element# using list comprehension   # initializing list  test_list = [1, 4, 5, 6, 7, 3]  # printing original listprint ("The original list is : " + str(test_list))  # using list comprehension# to assign variables from list elementvar1, var2, var3 = [test_list[i] for i in (1, 3, 5)]  # printing resultprint ("The variables are : " +  str(var1) +                            " " + str(var2) +                            " " + str(var3))
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 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 listprint ("The original list is : " + str(test_list))  # using using itemgetter()# to assign variables from list elementvar1, var2, var3 = itemgetter(1, 3, 5)(test_list)  # printing resultprint ("The variables are : " +  str(var1) +                           " " + str(var2) +                            " " + str(var3))
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 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 listprint ("The original list is : " + str(test_list))  # using using itertools.compress()# to assign variables from list elementvar1, var2, var3 = compress(test_list, (0, 1, 0, 1, 0, 1, 0))  # printing resultprint ("The variables are : " +  str(var1) +                            " " + str(var2) +                            " " + str(var3))
The original list is : [1, 4, 5, 6, 7, 3]
The variables are : 4 6 3

Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Defaultdict in Python
Python | Split string into list of characters
Python | Get dictionary keys as a list
Python | Convert a list to dictionary