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?

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)

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'}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.