1

I'm new to this so my title may be misleading and/or make no sense.

I have 32 objects I want to initialize with a loop. I have the desired names of the objects in one list, call it A. And the desired values in one list, call it B.

i want to do something like:

for(a,b) in zip(A,B):
a = b

Obviously this won't work, as it will just change the contents of list A.

For clarity, if list A was defined like:

A = [
'Game'
'Developer'
]

and B like:

B = [
'Super Mario 64'
'Nintendo'
]

I would want a loop that would execute the code:

Game = 'Super Mario 64'
Developer = 'Nintendo'

How do I do this?

PumpMan
  • 11
  • 2
  • 1
    in this case, `dict(zip(A, B))` – wjandrea May 18 '20 at 20:53
  • Don't do this. There are only hacky ways to accomplish this in the global scope, and it isn't really possible in local scopes. If you want to map strings objects to other objects, **use a dictionary**, that's what `dict` objects are, maps. – juanpa.arrivillaga May 18 '20 at 21:01
  • @PumpMan What you're trying to accomplish is a bad idea. The good equivalent is to use a dict. – wjandrea May 18 '20 at 21:23
  • @PumpMan Sorry, one thing I missed is that the answers on the linked question don't cover why it's a bad idea, but the comments do. – wjandrea May 18 '20 at 21:30
  • @juanpa Hey the top answer on that question mentions "the security risk" but doesn't say what it is. Would you mind editing it to explain? That user is no longer on SO so he probably won't mind. I would do it but I'm not sure exactly what it means - maybe it means that user input could rewrite variables, but I'm not sure where the input comes from. Also mentioning the maintenance aspect [Glenn covers in a comment](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables#comment1212436_1373164) would help too. – wjandrea May 18 '20 at 21:55
  • 1
    "Obviously this won't work, as it will just change the contents of list A." Actually, it **won't** change the contents of `A`. In any case, the link helps you because it tells you to use a `dict`, and not to dynamically create variables, which is a terrible design choice. – juanpa.arrivillaga May 18 '20 at 22:26

1 Answers1

-3

You can use globals() to do that.

A = [
    'Game',
    'Developer'
]

B = [
    'Super Mario 64',
    'Nintendo'
]

for(a, b) in zip(A, B):
    globals()[a] = b

print(Game)
print(Developer)
Super Mario 64
Nintendo
ywbaek
  • 2,971
  • 3
  • 9
  • 28
  • 2
    @PumpMan Don't use this, it makes your code harder to understand. Use a dict instead. – wjandrea May 18 '20 at 21:02
  • This was much easier for me to understand than dict so I will use this – PumpMan May 18 '20 at 21:05
  • @wjandrea He didn't ask for best practices. This does exactly what he asked for. And of course, I agree with you in that doing this is not recommended, but again, it does exactly what OP asked for. – ywbaek May 18 '20 at 21:18
  • 2
    @ywbaek Yes it does exactly what OP asked for, but that doesn't mean it's a good idea. If it's not recommended, why are you recommending it? At least put a warning at the top of your answer. – wjandrea May 18 '20 at 21:27
  • It works perfectly for what I am trying to do. I didn't post the exact nuances for what code I am creating or what purpose I am creating it for. And it is also a very simple solution. So I do not see a problem with it... – PumpMan May 18 '20 at 22:03
  • @wjandrea exactly OP's point. – ywbaek May 18 '20 at 23:15