I’m running the following code on a list of strings to return a list of its words:
words = [re.split('\\s+', line) for line in lines]
However, I end up getting something like:
[['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...]
As opposed to the desired:
['import', 're', '', '', '', 'def', 'word_count(filename):', '', ...]
How can I unpack the lists re.split('\\s+', line) produces in the above list comprehension? Naïvely, I tried using * but that doesn’t work.
(I’m looking for a simple and Pythonic way of doing; I was tempted to write a function but I’m sure the language accommodates for this issue.)
This will give you an iterator that can be used for looping through all words:
Creating a list instead of an iterator is just a matter of wrapping the iterator in a
listcall: