0

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

2 Answers 2

1

You're not getting a nested list because you only have one pair of [] in your list comprehenstion (and the expression you have there doesn't produce a list itself). Just do this instead (note that I've had to swap the order of your for clauses as well as insert the inner []s):

transactions_clean = [[st.strip('\n').strip() for st in transaction] for transaction in daily_transactions_split]
Sign up to request clarification or add additional context in comments.

1 Comment

It works. I tried going for the extra pair of [ ] earlier but it was of no avail, I did not flip the order of my for clauses. Thanks man
0

The pattern [(x, y) for x in xs for y in ys] is the equivalent of itertools.product(xs, ys).

The comprehension you are writing doesn't require a product of two collections.

import itertools

items = [(x, y) for x, y in itertools.product(range(5), range(10, 15))]
print(len(items), items)

Note the new collection is 5 * 5 in length, i.e. the product of the number of items in each collection.

OUTPUT:

25 [(0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (2, 10), (2, 11), (2, 12), (2, 13), (2, 14), (3, 10), (3, 11), (3, 12), (3, 13), (3, 14), (4, 10), (4, 11), (4, 12), (4, 13), (4, 14)]

import random
from string import whitespace

example_data =[
    ['Edith Mcbride', '$1.21', 'white', '09/15/17'],
    ['Herbert Tran', '$7.29', 'white&blue', '09/15/17'],
]

# Make the data dirty again.
transactions_dirty = [[f'{random.choice(whitespace) * random.randint(1, 4)}{item}{random.choice(whitespace) * random.randint(1, 4)}'
                       for item in transaction]
                      for transaction in example_data]
print(transactions_dirty)

# Clean the data.
transactions_clean = [[item.strip() for item in transaction]
                      for transaction in transactions_dirty]
print(transactions_clean)

OUTPUT:

[['\x0b\x0b\x0b\x0bEdith Mcbride\r\r\r', '\x0c\x0c$1.21\x0b', '\x0bwhite ', '\n\n09/15/17\t\t'], ['\t\t\tHerbert Tran\x0c\x0c\x0c', '\t\t$7.29\n\n\n', ' white&blue\r', '\n09/15/17 ']]

[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran', '$7.29', 'white&blue', '09/15/17']]

Note that .strip('\n').strip() does the same thing as .strip()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.