Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - are there other ways to apply a function and filter in a list comprehension?

this has been irking me for years.

given I have a list of words :

words = [ 'one', 'two', 'three', '', ' four', 'five ', 'six', \
         'seven', 'eight ', ' nine', 'ten', '']

even though it's super lightweight, I still feel weird writing this list comprehension:

cleaned = [ i.strip() for i in words if i.strip() ]

i don't like applying strip() twice. it just seems silly.

it's slightly/negligibly faster like this:

_words = [ w.strip() for w in words ]
cleaned = [ w for w in _words if w ]

which is also the same as

cleaned = [ i for i in [ w.strip() for w in words ] if i ]

I'm wondering if there are other ways to write this.

I was largely interested in a nested loops form of list comprehensions ( see Idiom for flattening a shallow nested list: how does it work? ) , but I couldn't figure anything out.

update

I put benchmark up on github, outlining my original 3 approaches, and ones shared below.

  • https://gist.github.com/jvanasco/8793879

The fastest is @Martijn Pieters filter(); converting the inner list to a generator expression is a negligible hit to speed, but should be better for memory management (according to python's docs ).

All the speed differences involved are , expectedly, negligible and not worth sharing.

like image 609
Jonathan Vanasco Avatar asked Feb 03 '14 21:02

Jonathan Vanasco


People also ask

Can we use function in list comprehension?

Answer. Yes, the variable in the for of a list comprehension can be used as a parameter to a function.

Which is faster filter or list comprehension?

The list comprehension method is slightly faster. This is, as we expected, from saving time not calling the append function. The map and filter function do not show a significant speed increase compared to the pure Python loop.

Can we use lambda in list comprehension?

List Comprehension Python Vs. In Python, we also use Lambda functions to modify and manipulate lists. Lambda functions are also known as anonymous functions. Lambda functions are usually used with various built-in functions such as map() filter(), and reduce() to work on lists.


2 Answers

A generator expression:

cleaned = [i for i in (word.strip() for word in words) if i]

Using filter() and map():

cleaned = filter(None, map(str.strip, words))

The latter produces a generator in Python 3; apply list() to it or combine map() with a list comprehension:

cleaned = [i for i in map(str.strip, words) if i]
like image 96
Martijn Pieters Avatar answered Nov 12 '22 00:11

Martijn Pieters


I have a slight variation, where I create a single valued temporary list:

>>> cleaned = [stripped for word in words
...            for stripped in [word.strip()]
...            if stripped]

More generally:

>>> values = [transformed for value in sequence
              for transformed in [transform(value)]
              if want(transformed)]
like image 35
Peter Wood Avatar answered Nov 12 '22 02:11

Peter Wood