I'm performing an exercise on CodeCademy revolving string manipulation, and I tried performing the task using list comprehension but couldn't manage it correctly: the correct way to perform it is to have a nested list, each consisting of the stripped strings. Here's how I managed to do it correctly:
transactions_clean = []
for transaction in daily_transactions_split:
new_trans = []
for i in range(len(transaction)):
new_trans.append(transaction[i].strip('\n').strip())
transactions_clean.append(new_trans)
This yields the result:
[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran', '$7.29', 'white&blue', '09/15/17']]
which is exactly what I'm looking for.
This method uses a nested loop, and I was interested in performing it using a list comprehension. I managed to come up with:
transactions_clean = [st.strip('\n').strip() for transaction in daily_transactions_split for st in transaction]
which manages to strip the strings correctly, but creates a non-nested list of strings. Could my goal be achieved using comprehensions?
Thanks