0

I have a bunch of variables as below.Now, I want to build a variable dynamically as below by iterating through for loop but surprisingly this won't work as .format can only be implemented for strings. Could anyone share your thoughts like how can this be implemented in Py? Any help would be appreciated. Thank you!

build_a="123"
build_b="456"
build_c="789"

build_src = ['a','b','c']
build_list = {}

for word in build_src:
    build_list[word] = build_{word}.format(word=word)
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Teja
  • 13,214
  • 36
  • 93
  • 155
  • 1
    Any particular reason why you are doing this instead of using a dictionary? – Havenard Apr 26 '19 at 06:06
  • build_a,build_b,build_c are variables which have huge strings....the example I have posted is simple one – Teja Apr 26 '19 at 06:08
  • What is your expected output? – Ora Aff Apr 26 '19 at 06:09
  • 2
    The question remains. Use a dictionary, it exists to solve problems like this. – Havenard Apr 26 '19 at 06:10
  • Do not use dynamic variables. *Just use a container*, likes a `list` or a `dict` – juanpa.arrivillaga Apr 26 '19 at 06:10
  • What is the output you want? – Devesh Kumar Singh Apr 26 '19 at 06:11
  • The syntax you show here is incorrect. The format mini language works only on strings. `build_{word}.format(foo)` will cause s syntax error. Sure there are unholy hacks to achieve what you want (eval and exec based incantations come to mind). But as others have pointed out, what you need is a suitable data structure and a dictionary is fine for this case. On the other hand, if you are exploring code generation or the like, simply build a string of the form you want then call on exec to bring ye beast to life. – Xero Smith Apr 26 '19 at 06:26

4 Answers4

2

A dictionary might be much better suited for what you're trying to achieve.

builds = {'a': "123", 'b': "456", 'c': "789"}
build_src = ['a','b','c']
build_list = {}
for word in build_src:
    build_list[word] = builds.get(word, None)
print build_list

Output: {'a': '123', 'c': '789', 'b': '456'}

pixie999
  • 468
  • 5
  • 11
2

Is this what you want:

build_src = ['a','b','c']
build_list = {}

for word in build_src:
    build_list[word] = 'build_'+word.format(word=word)
print(build_list)

output:

{'a': 'build_a', 'b': 'build_b', 'c': 'build_c'}
Ora Aff
  • 640
  • 1
  • 9
  • 24
0

you can use something like this :

build_a="123"
build_b="456"
build_c="789"

build_src = ['a','b','c']
build_list = {}

for word in build_src:
    globals()['build_new_var%s' % word] = word

it will give you what you want, something like this :

print(build_new_var_a)
print(build_new_var_b)

and the output will be :

a
b

but using dictionary would be a better solution , but if you need this for a particular reason you can do this too .

hossein hayati
  • 1,088
  • 2
  • 15
  • 34
0

As suggested in most of the answers a dict is the solution to your problems.

Option 1: through a pair of lists thanks to zip

In this case the tuple is formed by the zip function which returns an iterator which is consumed by the dict. This iterator merges the 2 lists by preserving the item's index.

Note: if you are planing to use 2 lists with different length you might be interested in having a look to zip_longest.

keys = ['a', 'b', 'c']
values = [123, 456, 789]

build_src = zip(keys, values)
build_list = dict(build_src)

Option 2: using dict comprehension

You can use a dict comprehension in case you want to explicitly show the loop during the dict construction process.

keys = ['a', 'b', 'c']
values = [123, 456, 789]

build_src = zip(keys, values)
build_list = {key: val for key, val in build_src}
Community
  • 1
  • 1
jesteras
  • 141
  • 1
  • 4