Search code examples
pythonlistlist-comprehensionstrip

more then one expression in list comprehension?


i want to use a list comprehension to split elements of a list.

line = [x.split(", ") for x in lineList]

At same time I'd like to remove tailing and leading characters of the elements (.rstrip('"')/.lstrip('"').

But a 'list' object has no attribute 'rstrip'. Is there a way to achieve this within a comprehension or do i have to go for a for-loop?

Thanks in advance for any advice, Lars


Solution

  • If you want to remove both leading and trailing quotes, why not using strip() directly? Also, if you want to flatten the list of lists:

    line = sum([[y.strip('"') for y in x.split(", ")] for x in lineList], [])