Nov-25-2019, 08:09 AM
(Nov-24-2019, 04:31 PM)paul41 Wrote: In my data set I seem to have some whitespace in some of the items. Do you know how i can remove this within the same for loop?
If the whitespaces are around arrow then code above takes care of that (it breaks at ' -> '). If there are additional whitespaces (at the beginning or end) then they could be stripped:
>>> lst = ['A -> B ', ' C -> D', ' E -> F '] # note spaces at start and end >>> new = [] >>> for item in lst: ... new.extend(chunk.strip() for chunk in item.split(' -> ')) ... >>> new ['A', 'B', 'C', 'D', 'E', 'F'] >>> [chunk.strip() for item in lst for chunk in item.split(' -> ')] # same result with list comprehension ['A', 'B', 'C', 'D', 'E', 'F']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.