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

Python how do I append to every string in list using lambda function?

I’m trying to learn about lambda functions, I’m would like the code to:

  1. add a slash ‘/’ to the end of a string in list ‘techCodeList’
  2. add and append every item in ‘finishList’ to the every entry in ‘techCodeList’, appending to ‘combinedCodeList’ every time (so combinedCodeList = [‘a/ABUL’, ‘a/BEL’, ‘a/PBL’] etc)

I could do it using other methods but I want to try using lambda, so would it be viable and if so how would I do it? My code is below:

#example shortened
class myClass:
    def __init__(self):
        self.techCodeList = ('a','b','c')

        def applyCodes(self):
                self.combinedCodeList = list()
                finishList = ["ABUL","BEL","PBL","PBUL","ABL","SBL","SBSL","SBUL","PNP","SNP","PCP","SCP","NBP","ASP","ACP","SAL","SAS","AMB","CBP","HBN","MBL","MWL","HBB","SPE","PBUL/SAMPLE"]#list to append to techcodelist
                len1 = len(self.combinedCodeList)
                arrayCounter = 0
                for i in self.techCodeList:
                    for _ in finishList:
                        print (arrayCounter)
                        self.techCodeList = list(map(lambda orig_string: orig_string + '/', self.techCodeList[arrayCounter]))
                        self.techCodeList = list(map(lambda orig_string: orig_string + finishList[arrayCounter], self.techCodeList[arrayCounter]))
                        self.combinedCodeList.append(self.techCodeList[arrayCounter])
                        if arrayCounter == len(self.techCodeList) - 1:
                            break
                        arrayCounter = arrayCounter + 1

                print (self.combinedCodeList)

myClass()

And here is the result in the combinedCodeList:

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

[‘aABUL’, ‘/BEL’, ‘/BEL’, ‘/BEL’]

If you have any other suggestions for good habits or suggestions for my code please feel free to leave them too, I’m still very much learning. Thanks.

>Solution :

If I understood correctly you want to create an exchaustive combinations between all the entries from the tech code and finish lists.

For that you can use list comprehension like below:

tech_code_list = ["a", "b", "c"]
finish_list = ["ABUL","BEL","PBL","PBUL","ABL","SBL","SBSL","SBUL","PNP","SNP","PCP","SCP","NBP","ASP","ACP","SAL","SAS","AMB","CBP","HBN","MBL","MWL","HBB","SPE","PBUL/SAMPLE"]
               

combined_code_list = [
    tech_code + "/" + finish for tech_code in tech_code_list for finish in finish_list
]

print(combined_code_list) 
# will print: ['a/ABUL', 'a/BEL', 'a/PBL', 'a/PBUL', ... ]
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