You can work on the entire dataframe without iteration. Access the underlying array and convert it into a list of lists in one go.
import pandas as pd
df = pd.DataFrame({"R1": [8,2,3], "R2": [-21,-24,4], "R3": [-9,46,6]})
#out = df.to_numpy().tolist() for pandas >0.24
out = df.values.tolist()
print(out)
print(out[0])
print(out[1])
print(out[2])
Output:
[[8, -21, -9], [2, -24, 46], [3, 4, 6]]
[8, -21, -9]
[2, -24, 46]
[3, 4, 6]
In this manner, you can use the out
variable as a collection of all individual lists that you wanted.
If you wish to use a dictionary instead, you can also create that as follows:
out_dict = {f"list{i}":lst for i, lst in enumerate(out, start=1)}
#Output:
{'list1': [8, -21, -9], 'list2': [2, -24, 46], 'list3': [3, 4, 6]}
print(out_dict['list1'])
print(out_dict['list2'])
print(out_dict['list3'])
Between the list and dict approaches, you should be able to cover all use-cases you really need. It is generally a bad idea to try making a variable number of variables on the fly. Related read