Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace empty string(s) in tuple

Tags:

python

Is there an easy way (hopefully a one liner) to replace '' with something like '-'?
Many thanks.

tup = (1,2,'ABC','','','','text')
like image 285
DGT Avatar asked Mar 04 '11 22:03

DGT


1 Answers

How about the following?

 tuple('-' if x == '' else x for x in tup)

As Felix Kling comments, tuples are immutable, so the best you can do is to return a new one.

like image 79
Mark Longair Avatar answered Sep 17 '22 08:09

Mark Longair