yeNniE
yeNniE

Reputation: 35

Any better way to write list comprehension?

I'm trying to remove whitespace and 'the' from an artist name. But my list comprehension seems not readable.

Is there any better way to write this list comprehension or I just use classic for loop?

 new_list= [artist.lower().replace(' ','') if artist[0:3] != 'the' else artist.lower().replace(' ','')[3:] for artist in artist_list]

Upvotes: 1

Views: 125

Answers (3)

Raymond Hettinger
Raymond Hettinger

Reputation: 226316

You were right in thinking that if the expression in a list comprehension gets too big, it would be better to use a straight for-loop.

That said, the expression can be simplified by applying str.replace() to remove 'the' without a test (replace is silent if there are no replacements):

>>> s = 'The Artist Formerly Known as Prince'
>>> s.lower().replace('the ', '').replace(' ', '')
'artistformerlyknownasprince'

Alternatively, the whole process might be simpler and more flexible with a regular expression:

>>> re.sub(r'(^the)|\s', '', s.lower())
'artistformerlyknownasprince'

Upvotes: 2

Kirk Strauser
Kirk Strauser

Reputation: 30947

It's still not illegal to pull your tricky bits out into a function:

def cleanup(artist):
    artist = artist.lower().replace(' ', '')
    if artist.startswith('the'):
        artist = artist[3:]
    return artist


new_list = [cleanup(artist) for artist in artist_list]

This has the big advantages (in my opinion) that you can test and debug the parts separately and that it's very clear what each step is doing.

BTW, you'll want to test and debug that cleanup portion right off the bat, as that approach will do weird things to They Might Be Giants.

Upvotes: 2

Selcuk
Selcuk

Reputation: 59184

You can combine a comprehension with a regex:

import re
new_list = [re.sub("^the ", "", artist.lower()).replace(" ", "") for artist in artist_list]

Upvotes: 0

Related Questions