The inverse of .join

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jun 18 01:02:09 EDT 2010


On Thu, 17 Jun 2010 20:03:42 +0000, Neil Cerutti wrote:

> I'm currently using the following without problems, while reading a data
> file. One of the fields is a comma separated list, and may be empty.
> 
>   f = rec['codes']
>   if f == "":
>     f = []
>   else:
>     f = f.split(",")
> 
> I just wondered if something smoother was available.

Seems pretty smooth to me. What's wrong with it? I assume you've put it 
into a function for ease of use and reduction of code duplication.

You could also use the ternary operator, in which case it's a mere two-
liner and short enough to inline wherever you need it:

f = rec['codes']
f = f.split(",") if f else []




-- 
Steven



More information about the Python-list mailing list