Thread Shed - wanting lists inside a list after stripping with list comprehensions

At step 8, the project asks one to remove the whitespace from the items inside of the list which are inside a overarching list.


I originally came up with this code here:
transations_clean = [item.strip() for lst in daily_transactions_split for item in lst]

While this does a good job or removing the whitespace, it removes one of the levels of lists and instead takes all the items out of the inner lists and puts them into the transactions list. This makes the next part of dividing up the customers, sales, and thread sold for each transaction more difficult (though I suppose you could perform some function which grabs every 4th of the first transaction item to get the value for each next transaction, which seems over doing it, example shown below).

Namely adding this after:

customers = [transactions_clean[i] for i in range(0, len(transactions_clean), 4)]

sales = [transactions_clean[i] for i in range(1, len(transactions_clean), 4)]

thread_sold = [transactions_clean[i] for i in range(2, len(transactions_clean), 4)]

*** SIDE NOTE *** I suppose this isn’t much “over doing it” but I went back to previous exercises in the course trying to find another, where the list comprehension would’ve been something like this: customers = [transaction[0] for transaction in transaction_clean] , which appears much simpler to me to use. *** END OF SIDE NOTE***

Next I tried: transations_clean = [lst for lst in daily_transactions_split for item.strip() in lst]

But got an error.

Next I tried: transations_clean = [lst[i].strip() for lst in daily_transactions_split for i in range(len(lst))]

Which I definitely thought would work but just did the same thing as my first try, removing the whitespace but putting the items into one big list instead of an inner list inside the main list.

Is there anyway to do this in a list comprehension? Or is the function of a list comprehension inherently unable to perform such a task? Perhaps because its putting a single item into a list and there’s no way of creating lists inside lists of a list comprehension?

I also tried after posting: transations_clean = [[lst[i].strip()] for lst in daily_transactions_split for i in range(len(lst))]

But this put each item into its own list which had only itself in it, which makes complete sense, however I was hoping the range i part would have the 4 items inside the list. I suspect this is close to the solution if there is one?

*** SOLUTION ***

I reset my Python 3 course and tried to go through it without any help from solutions or video walkthroughs. Ended up just going through the video I found my answer after a little bit of tinkering in the list comprehension. That being:

transactions_clean = [[data.strip() for data in transaction] for transaction in daily_transactions_split]

Showing that you need to simply put a list comprehension IN the list comprehension, who woulda thought. Paradigms are a pain in the neck sometimes especially for beginners.

Which then allowed me to do the:

customers = [transaction[0] for transaction in transactions_clean]
sales = [transaction[1] for transaction in transactions_clean]
thread_sold = [transaction[2] for transaction in transactions_clean]

Which was also shown in the video (however I already knew), my apologies I will keep trying until all options are exhausted before posting next time!


I had the same issue! Plus I tried a for loop in a for loop (as they suggested in a hint) and it printed anyway only one list of strings . only list in a list comprehension worked in the end. It got me almost crazy!
P.S. thanks for your post, it helped a lot.

1 Like