For example, suppose we have a string:
'abcdefg'
And we need to get a list like this:
['ab', 'bc', 'cd', 'de', 'ef', 'fg']
we should not use any kind of library
Here is my solution:
def str_split(s):
s = iter(s)
ch1=''
ch2=''
chars_list=[]
while True:
try:
ch1 = ch2 or next(s)
ch2 = next(s)
chars_list.append(ch1 + ch2)
except:
break
return chars_list
I wonder is there a better solution? Maybe it is possible to use list comprehension like here?