DGT
DGT

Reputation: 2654

replace empty string(s) in tuple

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

tup = (1,2,'ABC','','','','text')

Upvotes: 5

Views: 4831

Answers (1)

Mark Longair
Mark Longair

Reputation: 467591

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.

Upvotes: 16

Related Questions