I have no experience 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 will reorganize
return [doSomething(x) for x in originalList]
But suppose this is a little different, and my loop looks like this:
accumulationList = [] for x in originalList: y = doSomething(x) accumulationList.extend(y) return accumulationList
where doSomething list returns a list. What is the most pythonic way to achieve this? Obviously, a previous understanding of the list gave a list of lists.
python list-comprehension
Eric Wilson
source share