I’m stuck on task 8, where it asks me to clean up the strings within a list which is, itself, within another list.
My code:
transactions_clean = []
for i in daily_transactions_split:
for e in i:
transactions_clean.append(e.strip())
The above creates a new list with all the strings stripped of white space. But it gets rid of the original lists within the list. I would like to keep the original structure intact, but with cleaned internal list.
Must’ve spent 2 hours trying to figure this out. So help would be very much appreciated.
So, consider what is happening. You take each element of a list which is also a list, then you iterate though the elements of that list, strip the white space, and append them to the new list.
What you are wanting to do is strip the white space from each element inside the nested list, and then append the entire list to the new list, so that you end up with a list of lists like you originally had.
Do you see the difference in what I wrote? The second part includes some extra steps. Consider how you could make changes to each element of the nested list, and then append the list.