Error when appending to an empty list in python

Question:

I get:

'NoneType' object has no attribute 'append'

I want to start with an empty list:

Edate = []

But Q1) how do I define this to be a list that will take in dates?

Im not sure, so I input a timstamp to get me started:

Edate = [Timestamp('2018-01-01 00:00:00')]

which is now a 1 element list

Now, I want to append dates:

dates=

 1    2018-01-29
 2    2017-10-11
 3    2017-03-28
 4    2016-10-25
 5    2016-03-02
 6    2015-11-04
 7    2015-10-22
 8    2014-01-24
 9    2014-01-03
 10   2013-10-09

But in trying to do so, I encounter the aforementioned error, which I don’t understand. Thanks

PS- I would also like to do the same for numbers:

entrynumbers = []

data=


0        NaN
1    -31.336
2    -36.012
3    -21.282
4    -41.859
5    -31.381
6    -30.789
7    -27.509

entrynumbers = entrynumbers.append(data)
Asked By: Junaid Mohammad

||

Answers:

This line is the culprit:

entrynumbers = entrynumbers.append(data)

list.append mutates the list it is called on, but returns None. So do not reassign, just do:

entrynumbers.append(data)

For Q1, Python list is not parametrized like collection types in other languages. Even if you add an intial TimeStamp object, you will still be able to add objects of any other type later on.

Answered By: user2390182
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.