1

I know that in Python there's the possibility of converting a given string into a list:

>>> h='ghilmnoPqzH'
>>> list(h)
['g', 'h', 'i', 'l', 'm', 'n', 'o', 'P', 'q', 'z', 'H']

But how can I split the list into given intervals? I.e., given the above string h if I set a parameter s=4 I should get

['ghi', 'lmn', 'oPq', 'zH']

This question is different from How do you split a list into evenly sized chunks in Python? because, if I use that function I get the following error:

<generator object chunks at 0x0296A030>

2

1 Answer 1

1
>>> interval = 3
>>> s = 'ghilmnoPqzH'
>>> [s[start:start + interval] for start in range(0, len(s), interval)]
['ghi', 'lmn', 'oPq', 'zH']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.