So I’m reading a CSV file with two columns, and then taking the product of all of them.
What I am trying to do is convert t his product from a list of tuples, to a list of lists. I ultimately want to combine each tuple into a string with a comma. Here’s my code:
rowa = []
rowb = []
for row in csvfile:
if row[0] != "":
rowa.append(row[0])
if row[1] != "":
rowb.append(row[1])
wordlist = list(product(rowa, rowb))
And my pseudocode is this:
for x in wordlist:
x = list(x)
x.join(" ")
print wordlist should bring up a list of strings, which will be the old tuples, turned into a list and then joined together with a space
The join method works the other way around; call it on the space string:
It works on any sequence, so there is no need to convert tuples to lists here.
Note that you can dispense with the
!= ""tests for your columns; the empty string is considered boolean False as well:Finally, because you are already iterating over the wordlist, just leave it a generator and dispense with the list call altogether.