Dynamic Allocation of Data Types and Variables in Python | by Bipin P. | Towards Data Science

Suppose you are given a list (list_main) with 10 elements. You have to divide it into 3 parts. The first part is allocated to list1, the second part to list2, and the third part to list3. This division or partition of the list is analogous to K-fold Cross-Validation. For the uninitiated, cross-validation is a resampling procedure to access the accuracy of a predictive model in practice. In K-fold cross-validation, the dataset is divided into training data (usually 80%) and test data (usually 20% and will remain unseen data). The training data is again divided into k-folds or partitions that inturn is considered training data and validation data. The model will be trained on training data and the validation of the model will be done using validation data.
The following code divides the list into 3 partitions or sections and allocates the elements of each section to a list.
list_main = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]folds = 3section = int(len(list_main)/folds)start_index = 0end_index = 0counter = 1for i in range(folds):    if counter == 1:        end_index += section        list1 = list_main[start_index:end_index]        counter += 1    elif counter == folds:        list3 = list_main[end_index: ]        counter += 1    elif counter < folds:        start_index = end_index        end_index += section        list2 = list_main[start_index:end_index]        counter += 1print(f'List1: {list1}')print(f'List2: {list2}')print(f'List3: {list3}')list1 = [1, 2, 3]list2 = [4, 5, 6]list3 = [7, 8, 9, 10]
The output:
List1: [1, 2, 3]List2: [4, 5, 6]List3: [7, 8, 9, 10]
However, the code is not generic and more of a hardcoded type. For instance, if the number of divisions(folds) of the list_main increased to 4, then another list(list4) is required to store list elements. If it was increased to 6, then a total of 6 lists are required altogether. To accommodate the changes based on the number of partitions, the code needs to be changed. Specifically, new lists need to be assigned. However, that leads to more complexities and confusion. For instance, if folds=4, then the second partition and third partition have the same condition in the above code (counter < folds).
It would have been better if we write a generic code that can allocate lists as the number of folds increases or decreases. The globals() function in Python made the seemingly impossible job possible. It returns the global symbol table as a dictionary.
print(globals()){'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x02A8C2D0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'demo_ref_globals.py', '__cached__': None, 'x'_ {...}}
The modified code:
The output:
Enter the no. of folds: 5List 0: [1, 2]List 1: [3, 4]List 2: [5, 6]List 3: [7, 8]List 4: [9, 10]
You can do this dynamic allocation(frankly speaking, I don’t know the appropriate term for the allotment of data types)with other data types like variables, tuple, set, etc. You can find the codes here.
We are all going through tough times. Uncertainty looms large over the future. However, every cloud has a silver lining. The pandemic has healed earth to a greater extent than any other mission by environmentalists. So I can’t help sharing this awesome meme that I received as forward from my friend in WhatsApp. I really want to thank the unknown creator of this meme for the creativity and the harsh reality that it was portraying about the world we live in.
As the world is limping forward through an unprecedented time of an endemic that has attained gigantic proportions, we humans have no choice but to resort to social distancing(whenever we venture out for essentials)and stay at home. While you are in self-imposed quarantine, upgrade your skills, and learn something new. Hope you could learn something new. Happy Coding!!!