Issue
I need to understand why :
years = range(2010,2016)
years.append(0)
is possible, returning :
[2010,2011,2012,2013,2014,2015,0]
and
years = range(2010,2016).append(0)
or
years = [0].extend(range(2010,2016))
doesn't work ?
I understand that it is a type error from the message I got. But I'd like to have a bit more explanations behind that.
Solution
You are storing the result of the list.append()
or list.extend()
method; both alter the list in place and return None
. They do not return the list object again.
Do not store the None
result; store the range()
result, then extend or append. Alternatively, use concatenation:
years = range(2010, 2016) + [0]
years = [0] + range(2010, 2016)
Note that I'm assuming you are using Python 2 (your first example would not work otherwise). In Python 3 range()
doesn't produce a list; you'd have to use the list()
function to convert it to one:
years = list(range(2010, 2016)) + [0]
years = [0] + list(range(2010, 2016))
Answered By - Martijn Pieters
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.