-3

I'm facing a strange issue related to copying array elements inside other arrays, let me explain with a code:

I'm loading a csv into a bidimensional array, if I do something like this:

e = [None] * 4
with open("file.csv") as f:
    for line in f:
        e=line.split(",")
        finalarray.append(e)

If I do this, in finalarray I got only the last csv entry repeated all the time, but, if I do this:

 with open("file.csv") as f:
        for line in f:
            finalarray.append(line.split(","))

works like a charm, but I cannot undestand why the second option works and the first one fails loading always the last csv entry.

Kind regards, and thanks in advance.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Ferran
  • 281
  • 3
  • 13

1 Answers1

-2

I think the problem is the e init.

We are using references to the same value, so, when you change the value, you change all the references. And the last value es the last entry in the CSV.

Ferran
  • 281
  • 3
  • 13