Say we have a list my_list=["a","b","c"]. I want to create empty lists as
my_list=["a","b","c"]
a=[] b=[] c=[]
so that I can add some elements to them according to their names.
Here you use a function execto execute a command as if it were entered by you, but you use ia dynamic variable as the name
exec
i
for i in my_list: exec(i+'=[]')
Keep in mind that this is not very safe to do.
Creating variables programmatically is a very bad idea. Create a dictionary instead of these names as keys:
my_lists = {key:[] for key in my_list}
Then you can add the following to them:
my_lists['a'].append(some_data)
, , , .
Source: https://habr.com/ru/post/1672194/More articles:Android Studio does not detect Android device in Windows 10 - androidСуммирование строк в сгруппированных pandas данных и возврат NaN - pythonBeta Fabric vs. Apple TestFlight for iOS Beta Distribution? - iosDojo dialog to display image and text - javascriptHow to work with ASP.NET Core - c #Regular expression for legs and inches - with decimals and fractions - jqueryHow to create a custom Swagger schema in Django Rest Framework? - python-3.xThe default type parameter in inheritance is scalaКак сохранить файлы журнала из контейнера докеров? - dockerWhy should an expression be in parentheses for an array - arraysAll Articles