Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create dynamic key from given variables

In my project the code supposed to call bitbucket repos and get all the available branches. My responsibility is create dict where keys will branch name and value will number of child branches.

listOfRepos =         ['repo/association/branch/master','repo/publish/branch/master']
listOfChildBranches = [["childBranch1","childBranch2","childBranch3"], 
                       ["working1","demo2","rebased3"]]

Required output is:

repo/association/branch/master=['childBranch1', 'childBranch2', 'childBranch3']
repo/publish/branch/master=['working1', 'demo2', 'rebased3']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As per your requirement, this code will work.

listOfRepos =         ["repo/association/branch/master","repo/publish/branch/master"]
listOfChildBranches = [["childBranch1","childBranch2","childBranch3"],
                           ["working1","demo2","rebased3"]]
newList = dict()
for i in range(0,len(listOfRepos)):
    newList[listOfRepos[i]] = listOfChildBranches[i]

print(listOfRepos[0]+"="+str(newList["repo/association/branch/master"]))
print(listOfRepos[1]+"="+str(newList["repo/publish/branch/master"]))

To be more dynamic :

for idx,i in enumerate(listOfRepos):
    print(i+"="+str(newList[str(listOfRepos[idx])]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading