0

For example, I have 2 lists ['name', 'age', 'id'] and ['John', '20', '441']. I want to get output:

name = John
age = 20
id = 441

That is, from the elements of 1 list to make the names of variables. Given that the elements of list 1 are always different.

Update: I want to get a variable name with value "John", age with value '20' and etc.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
FleXX
  • 129
  • 1
  • 7
  • 4
    Can you explain why would you need to create variables? Look for dictionaries. – Austin Feb 29 '20 at 16:35
  • Do you want to create the *variables*, or do you want to create the *code to create the variables*? I.e. is your desired output a variable ``name`` with value ``"John"``, or the string ``"name = 'John'"``? – MisterMiyagi Feb 29 '20 at 17:03
  • A variable name with value "John" – FleXX Feb 29 '20 at 17:18

2 Answers2

7

You can use a dictionary for this.

vars=['name', 'age', 'id']
dets=['John', '20', '441']
details={k:v for k,v in zip(vars,dets)}

You can do this instead of a dict comprehension.

dict(zip(vars,dets))

details['name']
#'John'
details['age']
#'20'

Not recommended but you can use globals.

for var, det in zip(vars,dets):
    globals()[var]=det

>>>name
'John'
>>>age
'20'
Community
  • 1
  • 1
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • 1
    A more compact way will be to use the `dict` constructor directly: `dict(zip(vars, dets))` – Tomerikoo Feb 29 '20 at 16:38
  • @Tomerikoo yes, but I actually posted the same but it wasn't well-recieved in this question https://stackoverflow.com/a/60464003/12416453 So, I posted this. I'll `dict(zip(vars,dets))` to the answer though as it seems more pythonic but some may argue about it. – Ch3steR Feb 29 '20 at 16:39
  • @Ch3steR,.. which does not mean the compact one is any worse. – Austin Feb 29 '20 at 16:41
  • I don't see any down-votes or negative comments... What makes you say it's not well-recieved? It is a prefectly Pythonic way of doing this – Tomerikoo Feb 29 '20 at 16:41
  • @Tomerikoo Actually they were retracted after a long debate about it. – Ch3steR Feb 29 '20 at 16:42
  • 1
    Ah I see. Well it might be because the situation there was a bit different as the dictionary was already existed so one might argue that it could lead to side effects. But here this is exactly the use-case for using the `dict(zip(...))` construct... – Tomerikoo Feb 29 '20 at 16:43
  • On second look at the accepted answer there, I really don't see why would anyone claim yours was wrong... Anyway, doing `{k: v for k,v in zip(x, y)}` is just the longer, explicit way of `dict(zip(x, y))` – Tomerikoo Feb 29 '20 at 16:47
  • @Tomerikoo Agreed, Good point. `zip` only zips until one of the iterable is consumed that may be the case for not using `zip` in that question. – Ch3steR Feb 29 '20 at 16:47
  • @Tomerikoo Maybe it just comes down to preferences. Some prefer explicit and some implicit but `{k:v for k,v in zip(x,y)}` isn't pythonic IMO. – Ch3steR Feb 29 '20 at 16:50
  • @Austin Completely agreed. And `dict(zip(vars,dets))` is more pythonic way to do IMO. – Ch3steR Feb 29 '20 at 16:51
  • Why is `globals()[var]=det` not recommended? I saw that being criticized [here](https://stackoverflow.com/questions/10806327/python-create-a-global-variable-from-a-string) too but I don't see why that would be wrong. – revliscano Feb 29 '20 at 17:29
  • @revliscano It's clarified [here.](https://stackoverflow.com/questions/13311033/creating-a-global-variable-from-a-string-from-within-a-class?rq=1) – Ch3steR Feb 29 '20 at 17:36
-2

Program:

label = ['name', 'age', 'id']
val = ['John', '20', '441']

for x,y in zip(label,val):
    print(x,y)

Output:

name John
age 20
id 441
night vision
  • 124
  • 5