python - 'NoneType' object is not iterable' error when slicing string to tuple using list & tuple conversion -


this question has answer here:

so here's function wrote slices string tuple without spaces.

def slice_tuple():     name = 'james bond'     b = name.partition(' ')     b = list(b)     b = b.remove(' ')     b = tuple(b)     return(b) 

this function throws error

typeerror: 'nonetype' object not iterable

at b = tuple(b). why this?

i think rather using 'partition' can use 'split' method, never create ' ' item. see below modified code of yours

def slice_tuple():    name = 'james bond'    b = name.split(' ')    b = tuple(b)    return(b) 

it results in

slice_tuple() out[6]: ('james', 'bond') 

Comments