I'm not experienced in Python, and I often write code that (simplified) looks like this:
accumulationList = []
for x in originalList:
y = doSomething(x)
accumulationList.append(y)
return accumulationList
Then after my test passes, I refactor to
return [doSomething(x) for x in originalList]
But suppose it turns out a little different, and my loop looks like this:
accumulationList = []
for x in originalList:
y = doSomething(x)
accumulationList.extend(y)
return accumulationList
where the doSomething
list returns a list. What is the most Pythonic way to accomplish this? Obviously, the previous list comprehension would give a list of lists.