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

For loop a values assignment

I have this dataset, where I will use to fill missing values.

In [1]: sex_class = ['female','male']
        pclass = [1,2,3]
        fill_values = [34,40,29,18,24,25]

In [2]: for i in pclass:
            for j in sex_class:
                print(i,j)
Out[2]: 1 female
        1 male
        2 female
        2 male
        3 female
        3 male

How can I make it so that the output will appear as:

1 female 34
1 male   40
2 female 29
2 male   18
3 female 24
3 male   25

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 could do with pop for the list

sex_class = ['female','male']
pclass = [1,2,3]
fill_values = [34,40,29,18,24,25]
for i in pclass:
     for j in sex_class:
            print(i,j,fill_values.pop(0))
1 female 34
1 male 40
2 female 29
2 male 18
3 female 24
3 male 25
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