-1

How do I split a list into sub-lists, and then further split that to make each character an independent value?

I need to do this all inside of Python, version 3.6.5. The list will be in integers.

I need to convert

[123, 456]

into

[[1,2,3], [4,5,6]]

EDIT:

And after I have done that, how do I print the first character of the first sublist?

Note: Splitting integer in Python? and Turn a single number into single digits Python don't solve or answer my problem, as they do not explain how to make sub-lists out of them.

  • More [here](https://stackoverflow.com/questions/1906717/splitting-integer-in-python). Sometimes, a little search helps and is quicker than posting question which shows no research effort and no attempt like yours – Sheldore Dec 29 '18 at 17:43
  • *"they do not explain how to make sub-lists"* - so what? They show you how to process one number, so then you just need to apply the same logic to each of them in the list. – jonrsharpe Dec 29 '18 at 17:51
  • What have *you tried* to get the first character of the first sub list, and what is the problem with it? This isn't a code-writing service. – jonrsharpe Dec 29 '18 at 18:21
  • Please don't change your question after it has been answered and you have accepted an answer. – jpp Dec 29 '18 at 21:01

2 Answers2

2

For example, using list comprehension:-

my_list = ['123', '456']
[list(x) for x in my_list]

If my_list is a list of integers:-

my_list = [123, 456]
[[int(x) for x in str(n)] for n in my_list]
Tom Ron
  • 5,906
  • 3
  • 22
  • 38
2

A simple list comprehension will do, the idea is iterate over the characters in the string representation of the numbers:

[[int(x) for x in str(s)] for s in l]
Netwave
  • 40,134
  • 6
  • 50
  • 93