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

How to Write in csv or Excel file using CSV module or Pandas by combining two lists into one In Python?

I’ve got two lists.

list1 = ['abc', 'bcd', 'efg', 'ijk', 'lpo']

list2 = [111, 222, 333]

Sometimes list1 is more values than list2, and other times list2 is more values than list1.

How am I able to write on a CSV File Like This.

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

abc 111

bcd 222

efg 333

ijk

lpo

enter image description here

>Solution :

You can try with itertools zip_longest then to_csv

import itertools
pd.DataFrame(itertools.zip_longest(list1,list2)).to_csv('Your.csv',header=None, index=None)
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