Open In App

Python – Construct variables of list elements

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with python lists, we can have a problem in which we need to convert each element in list to variables and the corresponding list as the value. This can have applications in many domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using dict() + zip() 

The combination of the above functions can be used to perform this task. In this, we zip both the list into a dictionary and key of the dictionary can act as a variable and value as a variable value. 

Python3




# Python3 code to demonstrate
# Construct variables of list elements
# using dict() + zip()
 
# Initializing lists
test_list1 = ['gfg', 'is', 'best']
test_list2 = [1, 2, 3]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Construct variables of list elements
# using dict() + zip()
res = dict(zip(test_list1, test_list2))
 
# printing result
print("Variable value 1 : " + str(res['gfg']))
print("Variable value 2 : " + str(res['best']))


Output : 

The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : [1, 2, 3]
Variable value 1 : 1
Variable value 2 : 3

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.

Method #2: Using global() + loop 

This is yet another way to initialize variables as lists. In this, we use global space to declare variables. 

Python3




# Python3 code to demonstrate
# Construct variables of list elements
# using globals() + loop
 
# Initializing lists
test_list1 = ['gfg', 'is', 'best']
test_list2 = [1, 2, 3]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Construct variables of list elements
# using globals() + loop
for var, val in zip(test_list1, test_list2):
    globals()[var] = val
 
# printing result
print("Variable value 1 : " + str(gfg))
print("Variable value 2 : " + str(best))


Output : 

The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : [1, 2, 3]
Variable value 1 : 1
Variable value 2 : 3

Time Complexity: O(n*n), where n is the length of the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list.

Method #3: Using List comprehension

  1. Iterate over the indices i in the range of the length of test_list1.
  2. For each index i, use the update() method with globals() to create a new variable with the name of the corresponding element in test_list1 and the value of the corresponding element in test_list2.
  3. Return the variables with names corresponding to the elements in test_list1 and values corresponding to the elements in test_list2.

Python3




# Initializing lists
test_list1 = ['gfg', 'is', 'best']
test_list2 = [1, 2, 3]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Construct variables of list elements using globals() and list comprehension
[globals().update({test_list1[i]: test_list2[i]}) for i in range(len(test_list1))]
 
# printing result
print("Variable value 1 : " + str(gfg))
print("Variable value 2 : " + str(best))
#This code is contributed by Vinay Pinjala.


Output

The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : [1, 2, 3]
Variable value 1 : 1
Variable value 2 : 3

Time complexity: O(n), where n is the length of the lists test_list1 and test_list2. This is because the list comprehension iterates over the indices of the lists, and the update() method takes constant time to create each new variable.

Auxiliary Space: O(n), where n is the length of the lists test_list1 and test_list2. This is because the globals() dictionary must store a key-value pair for each element in test_list1, and each key-value pair takes constant space.

Method #4:Using itertools and reduce modules:

Algorithm:

  1. Initialize two lists test_list1 and test_list2.
  2. Print the original lists.
  3. Use list comprehension to create a list of dictionaries where the key is the element in test_list1 and the value is the corresponding element in test_list2.
  4. Use itertools.chain() and reduce() functions to merge all dictionaries into a single dictionary.
  5. Use the globals() function to set the variables dynamically using the dictionary keys.
  6. Print the values of the variables.

Python3




import itertools
from functools import reduce
 
test_list1 = ['gfg', 'is', 'best']
test_list2 = [1, 2, 3]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Construct dictionary of list elements using itertools and reduce
result_dict = reduce(lambda x, y: dict(itertools.chain(x.items(), y.items())),
[{test_list1[i]: test_list2[i]} for i in range(len(test_list1))])
 
# Set variables dynamically using globals()
globals().update(result_dict)
 
# printing result
print("Variable value 1 : " + str(gfg))
print("Variable value 2 : " + str(best))
#This code is contributed by Jyothi pinjala.


Output

The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : [1, 2, 3]
Variable value 1 : 1
Variable value 2 : 3

Time complexity:

The time complexity of the code is O(n), where n is the length of test_list1.

Auxiliary Space:

The space complexity of the code is O(n), where n is the length of test_list1, because we are creating a list of n dictionaries.

Method #5: Using locals and string formatting:

Algorithm :

  1. Initialize two lists test_list1 and test_list2 with some values.
  2. Using a for loop, iterate through the range of length of test_list1.
  3. For each iteration, create a new local variable with the name of the element from test_list1 and assign it the corresponding value from test_list2.
  4. Print the values of the created variables.

Python3




test_list1 = ['gfg', 'is', 'best']
test_list2 = [1, 2, 3]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
for i in range(len(test_list1)):
    locals()[test_list1[i]] = test_list2[i]
 
print("Variable value 1 : " + str(gfg))
print("Variable value 2 : " + str(best))
# This code is contributed by Rayudu.


Output

The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : [1, 2, 3]
Variable value 1 : 1
Variable value 2 : 3

Time complexity:O(1) 
The time complexity of the for loop is O(n), where n is the length of test_list1. The time complexity of the print statements is O(1). Therefore, the overall time complexity of the code is O(n).

Auxiliary Space: O(N)
The space complexity of the code depends on the size of the input lists and the number of local variables created. In the worst case, the space complexity of this code is O(n), where n is the length of test_list1, as it creates n local variables. However, in practice, the space complexity may be lower than this, as the number of local variables created may be smaller than n.



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads