Dmitry
Dmitry

Reputation: 14622

Why can't I add items to the list in Python correctly?

I'm just trying to add items to the list and I can't! My code:

    import numpy as np
    data = [[]] * 10
    for i in range(0, len(data)):
        slice = np.random.randn(100, 100, 3)
        print('before: ', np.shape(data))
        print('slice: ', np.shape(slice))
        data[i].append(slice)
        print('after: ', np.shape(data))

Result is the following:

before:  (10, 0)
slice:  (100, 100, 3)
after:  (10, 1, 100, 100, 3)

...

before:  (10, 0)
slice:  (100, 100, 3)
after:  (10, 10, 100, 100, 3)

So, (100, 100, 3) wasn't inserted to the correct position in data[0], but additional dimension was created! Why? And how can I avoid it?

The correct dimension of the result list should be (10, 1, 100, 100, 3) - not (10, 10, 100, 100, 3).

Upvotes: 1

Views: 105

Answers (1)

cs95
cs95

Reputation: 402513

The main culprit is

data = [[]] * 10

Which creates a single empty list and replicates it 9 times more, so in effect you are inserting to the same list 10 times. For example:

In [113]: data = [[]] * 10

In [114]: data[0].append(1)

In [115]: data
Out[115]: [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

You should either

  1. create a place-holder list and then insert at the ith element, or
  2. create an empty list [] and call .append

But not both.

I would recommend declaring an empty list:

data = [[] for _ in range(10)]

Now, call .append in the loop:

data.append(slice)

You can convert the resultant to an array using data = np.array(data). Here's a sample:

data = [[] for _ in range(10)]

 for i in range(0, len(data)):
     slice = np.random.randn(100, 100, 3)
     data[i].append(slice)


print(np.array(data).shape)
(10, 1, 100, 100, 3)

Upvotes: 2

Related Questions