Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split list by certain repeated index in python

I have this list of decimal values

[97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112] // more and more...

How to split it in this way :

[[97,98,99,100],[101,102,103,104],[105,106,107,108],[109,110,111,112]]

then convert the decimal values to string so it can be like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['abcd','efgh','ijkl','mnop']

>Solution :

You can do it with list comprehension:

a = [97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112]
[''.join(chr(j) for j in a[i:i+4]) for i in range(0, len(a), 4)]

Output:

['abcd', 'efgh', 'ijkl', 'mnop']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading