Sorry for the awkward title but I’ll explain better here;
In the function below;
def returnList():
list = []
for i in xrange(4):
list.append(i)
return list
It returns the list [0,1,2,3]. In another function;
def returnAllLists():
totalList = []
for i in xrange(4):
totalList.append(returnList())
return totalList
As expected it turns a result like
[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
. The tricky part is that I need the result
[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]
. I can of course easily assign the result of returnAllList to another list and go for two loops and insert the elements individually into another list however I think a more efficient way could be done since my method has an O(N^2) complexity just for assigning same values in a different way. Any suggestions?
Use
extend()in place ofappend():Does exactly what you want.