[Tutor] Python "returning .split() without empty strings

Chris C mysecretrobotfactory at gmail.com
Fri Oct 23 20:44:20 EDT 2020


Hi all, I have a question regarding string operations.
the following string needs to be chopped up, having the 0's dividing the
data that I wanted, therefore the string here becomes

# run this
string = '1111022220000333300044405550055'
string = string.split('0')

# get this
['1111', '2222', '', '', '', '3333', '', '', '444', '555', '', '55']

# but I want it without the empty strings, so I ran another loop to pick up
# only what I wanted

key_list = list()

for key in string :
if key != '':
key_list.append(key)

print(key_list)


# the above is this
['1111', '2222', '3333', '444', '555', '55']


The point is to remove all the 0's, however many there are.
I hope I have explained it well, please let me know if I didn't.

So, the question is, is it possible to do this without the 2nd stage?

Thanks!


More information about the Tutor mailing list