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)
7
  • 1
    Any particular reason why you are doing this instead of using a dictionary? Commented Apr 26, 2019 at 6:06
  • build_a,build_b,build_c are variables which have huge strings....the example I have posted is simple one Commented Apr 26, 2019 at 6:08
  • What is your expected output? Commented Apr 26, 2019 at 6:09
  • 2
    The question remains. Use a dictionary, it exists to solve problems like this. Commented Apr 26, 2019 at 6:10
  • Do not use dynamic variables. Just use a container, likes a list or a dict Commented Apr 26, 2019 at 6:10

4 Answers 4

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'}

Sign up to request clarification or add additional context in comments.

Comments

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'}

Comments

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 .

Comments

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.