-1

I have a list as follows:

['ExampleName', ['1', 'some_value_1'], ['2', 'some_value_2']]

and I am trying to figure out how to write function or loop that takes the first element of the list and set it up as a dictionary name. The desired output would be equivalent to:

ExampleName = {1:'some_value_1', 2:'some_value_2'}

So far tried eval command without success

User0261
  • 3
  • 2
  • 5
    If you have several such structures, you should rather build a dict like `{'ExampleName': {1:'some_value_1', 2:'some_value_2'}, 'SomeOtherName': {....}, ...}`. Creating variables like you want to do is usually a bad idea. – Thierry Lathuille Jan 13 '20 at 19:03
  • 1
    Don't do this. Don't use dynamic variables, use another *container*, in this case, another `dict` probably – juanpa.arrivillaga Jan 13 '20 at 19:39

3 Answers3

0

Like said in the comments it's a really bad idea to generate variable names based on other variable content, you should create a dictionary, something like this:

example = ['ExampleName', ['1', 'some_value_1'], ['2', 'some_value_2']]

output = {
    example[0]: {element[0]: element[1] for element in example[1:]}
}
print(output)

>>> {'ExampleName': {'1': 'some_value_1', '2': 'some_value_2'}}


marcos
  • 4,473
  • 1
  • 10
  • 24
0

I don't think it's a good idea to do it like that. Using exec may lead to some potential security risks.

Anyway, it does exactly what you're trying to achieve:

example_list = ['ExampleName', ['1', 'some_value_1'], ['2', 'some_value_2']]

name, *key_value_pairs = example_list
dictionary = {int(key): value for key, value in key_value_pairs}
exec(f"{name} = {dictionary}")

Try it online!


Also, see:

-1

Note: this is not a good thing to do, just for fun.

You can create variables at runtime by using the locals() dictionary, for example:

>>> hello # this variable does not exist yet
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    hello
NameError: name 'hello' is not defined
>>> locals()['hello'] = 'world' # create new variable
>>> hello
'world'
>>> locals().pop('hello')  # delete variable
'world'
>>> hello
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    hello
NameError: name 'hello' is not defined
>>> 

Now by knowing this you should be able to build the dictionary you want

Hoxha Alban
  • 1,042
  • 1
  • 8
  • 12
  • Have a look at https://stackoverflow.com/questions/1450275/any-way-to-modify-locals-dictionary and https://stackoverflow.com/questions/4997184/why-is-it-bad-idea-to-modify-locals-in-python – Thierry Lathuille Jan 13 '20 at 19:19
  • No, `locals()` is exactly what you should'n't use even if you are trying to dynamically create variables, because assignment to `locals()` is not guaranteed to actually affect the local namespace. Indeed, in CPython, this will *only* work when `locals() is globals()`, i.e. when you are in the global namespace. It will fail inside a function, for example – juanpa.arrivillaga Jan 13 '20 at 19:41