Can this expression of understanding a Python list be simplified?

input = "foo ,,bar ,baz,"
tags = [x.strip() for x in input.split(',') if len(x.strip()) > 0] 

The desired output is obviously a list with no empty lines.

The question is in the spirit of micro-optimization; there is a way to not a strip()candidate xtwice, i.e. once for the test and once for adding?

To rephrase, can you create a value in expressions that can be added to the list without doing it twice?

+3
source share
3 answers

Creating a new line will always be more expensive than scanning. x.isspace()will be returned after the first space character

tags = [x.strip() for x in input.split(',') if x and not x.isspace()]
+9
source
text = 'foo ,,bar ,baz,'

( text, input, input - . .)

, len(x.strip()) > 0 ( ) x.strip().

tags = [x.strip() for x in text.split(',') if x.strip()]

, , , :

tags = [x for x in (x.strip() for x in text.split(',')) if x]

, ...

tags = filter(bool, map(lambda x: x.strip(), text.split(',')))

:

>>> from timeit import timeit
>>> timeit(lambda: [x.strip() for x in text.split(',') if x.strip()])
1.9443869590759277
>>> timeit(lambda: [x for x in (x.strip() for x in text.split(',')) if x])
2.1135239601135254
>>> timeit(lambda: filter(bool, map(lambda x: x.strip(), text.split(','))))
2.52907395362854

, .

+3

it works too ...

 text = "foo ,,bar ,baz,"
 text.replace(',',' ').split()
+1
source

All Articles