0

This seems to be a bit difficult to explain. I wish to create the name of a variable from the 1st index of the list and assign the list as its value

as an example:

#the input is a list like below,
['name_of_variable','value1','value2','value3','value4','value5']

#the output would be as below,
name_of_variable = ['value1','value2','value3','value4','value5']

Any suggestions or ideas would be much appreciated.

Thanks in advance

1 Answers1

2

You can use locals to create a named variable this way

>>> data = ['name_of_variable','value1','value2','value3','value4','value5']
>>> locals()[data[0]] = data[1:]
>>> name_of_variable
['value1', 'value2', 'value3', 'value4', 'value5']

Though as a design, I would personally discourage this. If you want to map a key to value(s) I would prefer to use a dict

>>> items = {data[0]: data[1:]}
>>> items
{'name_of_variable': ['value1', 'value2', 'value3', 'value4', 'value5']}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218