0

I have data that exports in a string

output = '012345678910abcdefghijkl'

cleaned_output = [output[index:index + 4] for index in range(0, len(output), 4)]
cleaned_output = [cleaned_output[i][item] for i in range(0, len(cleaned_output)) for item in range(0,len(cleaned_output[i]))]

Which returns:

['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']

However, I am looking to return the below, any ideas on where I am going wrong?

[['0', '1', '2', '3'], ['4', '5', '6', '7'], ['8', '9', '1', '0'], ['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]
1
  • 1
    You only need cleaned_output = [list(output[index:index + 4]) for index in range(0, len(output), 4)] Commented Apr 21, 2022 at 22:59

1 Answer 1

1

You should just split your input into chunks of 4 and then convert them directly to lists:

cleaned_output = [list(output[i:i+4]) for i in range(0, len(output), 4)]

Output:

[['0', '1', '2', '3'], ['4', '5', '6', '7'], ['8', '9', '1', '0'], ['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I was overcomplicating that, this is a great solution!
Yeah, you were basically there with your first line of code, just needed to add the list conversion...

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.