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

function returning list with one extra length than it should

I have data that looks like this:

data = [[['INS', 'Y', '18', '021', '28', 'A', '', '', 'AC'],
         ['REF', '0F', '816383217'],
         ['HD', '021', '', 'EPO', 'Copayment Level -1', 'IND']],
        [['INS', 'Y', '18', '024', '07', 'A', '', '', 'TE'],
         ['REF', '0F', '734419065']],
        [['INS', 'Y', '18', '024', '07', 'A', '', '', 'TE'],
         ['REF', '0F', '778954065']]]

I want to loop through it and create a pandas series of the copayment_level which is the number after the dash -. If that element does not exist in the list of lists then it should append None to the new list. I created a function to do this but the problem is it returns one extra than it should.

def copay_level():
    l = []
    for i in data:
        for lst in i:
            for x in lst:
                if x.startswith('Copayment Level'):
                    x = x.split('-')
                    l.append(x[1])
        else:
            l.append(None)
    return l

copayment_level = pd.Series(copay_level())
print(copayment_level)

0       1
1    None
2    None
3    None
dtype: object

Cant figure out why its doing this. Thanks

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 :

You have to avoid adding None if you found Copayment Level in the item. Use a variable to track this.

def copay_level():
    l = []
    for i in data:
        found_copay = False
        for lst in i:
            for x in lst:
                if x.startswith('Copayment Level'):
                    x = x.split('-')
                    l.append(x[1])
                    found_copay = True
        if not found_copay:
            l.append(None)
    return l

You could do this with else: if you only had one level of looping, and used break when you find a Copayment Level. But since that’s in a nested loop, else: won’t detect a break there.

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