A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like for the keyword in other programming languages and works more like an iterator method as found in other object-orientated programming languages. With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc.
Arrays are used to store multiple values in one single variable:
So how to apply all of this to our problems?
lst1 = ["from","insert","create"]
lst2 = ["_table","_list"]
for iter1 in lst1:
for iter2 in lst2:
print(iter1 + iter2)
then the output will be like :
This still not solving our problem because what we produce now is just a string, not a variable, even an empty list. so modify a bit your code like this which is adding locals at the front, and now you are good to go.
The locals() function in python is used to return the dictionary of the current local symbol table. The local symbol table is accessed with the help of the locals() function. This function works the same as the globals() function. The only difference between them is the locals() function access the local symbol table, and the globals() access the global symbol table, and both return the dictionary.
lst1 = ["from","insert","create"]
lst2 = ["_table","_list"]
for iter1 in lst1:
for iter2 in lst2:
locals()[iter1 + iter2] = []
then all of the variables are been declared automatically and stored inside the cached. so next time if you want to add or declare a new empty list, just add more rows inside the array whether the first array or the second array.
Thank you.
0 Comments