0

I need help on list append. I have to export it into CSV with the respective list index.

lst1 = ['a', 'b', 'c']
lst2 = ['w', 'f', 'g']
lst3 = ['e', 'r', 't']

ap = []

ap.append((lst1, lst2, lst3))

output: [(['a', 'b', 'c'], ['w', 'f', 'g'], ['e', 'r', 't'])]

Expected output:

[('a', 'w', 'e')
 ('b', 'f', 'r')
 ('c', 'g', 't')]

I need to export to Excel via Pandas, please help.

 col1   col2    col3
 a       w       e
 b       f       r
 c       g       t

1 Answer 1

3

You need a list of tuples, not a list of a tuple of lists. For your result, you can use zip with unpacking to extract items in an iterable of lists by index.

df = pd.DataFrame(list(zip(*(lst1, lst2, lst3))),
                  columns=['col1', 'col2', 'col3'])

print(df)

  col1 col2 col3
0    a    w    e
1    b    f    r
2    c    g    t

Then export to Excel as you normally would:

df.to_excel('file.xlsx', index=False)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.