0

I have following variable:

number = "456367"

I need to add it to list by one number, like this:

list = [['4', '5', '6', '3', '6', '7']]

How can I achieve this?

1
  • 1
    Do you really want a [['4', '5', '6', '3', '6', '7']] as your return value, as opposed to a ['4', '5', '6', '3', '6', '7']? Commented Oct 24, 2017 at 4:46

2 Answers 2

1

Since a string is iterable, pass it to the list constructor:

>>> list("456367")
['4', '5', '6', '3', '6', '7']

Which is why you would not want to name the result list because you would stomp on the name of that convenient function ;-)


If you really want [['4', '5', '6', '3', '6', '7']] vs just a list of characters:

>>> s="456367"
>>> map(list, zip(*s))
[['4', '5', '6', '3', '6', '7']]

Or,

>>> [list(s)]
[['4', '5', '6', '3', '6', '7']]

But I am assuming with the first answer that the extra brackets were a typo.

Sign up to request clarification or add additional context in comments.

Comments

0
>>> number = "456367"
>>> [char for char in number]
['4', '5', '6', '3', '6', '7']

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.