0

I have a set of data with two lists. list_2 can have any number of nested lists in it (which is where my problem stems from).

list_1 = [2000.0, 2100.0, 2200.0]
list_2 = [[-1.86, 0.49, 1.36], [-1.29, 0.59, 1.62], [-.99, 0.19, 1.76]]

once in the csv file it should look like

2000    -1.86    -1.29    -.99
2100    0.49    0.59     0.19
2200    1.36     1.62     1.76

I have tried a few different ways of writing the file

    with open(file_name, 'w') as file:
        writer = csv.writer(file)
        writer.writerows(zip(list_1, list_2))

I've also tried zipping them before as see in this question

I've also tried separating the lists (in list_2) but that does not allow for any growth. I do not know the number of nested lists before running the application so it has to be able to grow.

writer.writerows(zip(list_1, list_2, list_3, list_4))  # does not allow for dynamic growth
7
  • show how should look csv for a more deeper nesting, say 4 levels Commented Feb 13, 2023 at 21:02
  • 1
    If you don't know the number of lists, you should be using a list of lists, not individually numbered list variables. Commented Feb 13, 2023 at 21:02
  • @chepner Yes that's what I'm trying to do. The last example was just to show what I have tried and what didn't work for me Commented Feb 13, 2023 at 21:04
  • 1
    You have a list of data in column-major format. csv.writerows can write a row-major list-of-lists. You can do this by transposing your data. Does this answer your question? Transpose list of lists (just do col_data = [list_1, *list_2] first) Commented Feb 13, 2023 at 21:07
  • 2
    In each case pay attention to add newline='' in order to avoid empty rows: with open(file_name, 'w', newline='') Commented Feb 13, 2023 at 21:59

2 Answers 2

3

if I get you right you don't know how many lists you are going to have. But all of them will have the same size?

And you want them to be stored "vertically"?

I have the feeling a transpose operation would help you very well.

import csv
import numpy

list_1 = [2000.0, 2100.0, 2200.0]
list_2 = [[-1.86, 0.49, 1.36], [-1.29, 0.59, 1.62], [-.99, 0.19, 1.76]]

list_3 = [list_1] + list_2
output_array = numpy.transpose(numpy.array(list_3))

with open(file_name, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(output_array)
Sign up to request clarification or add additional context in comments.

1 Comment

@PranavHosangadi for me it is mostly enough to get an idea how to solve a situation. But if you ask I will extend the answer ;-)
1

You can do this with the * operator:

zip(list_1, *list_2)

This will create a list of lists where the first element of each sub list comes from list_1 and the remaining elements come from each of the lists in list_2.

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.