3

How would you split up the number 123456789 into [1,2,3,4,5,6,7,8,9] using Python?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
phighter
  • 49
  • 1
  • 1
  • 5

4 Answers4

10

One way is to turn the number into a string first and then map each character digit back to an integer:

>>> map(int, str(123456789))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

This does the following:

  • str turns the integer into a string: '123456789'

  • map applies the function int to each character in this string in turn, turning each one back into an integer value.

  • a list of these integers is returned.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
  • When using your method i get an error saying"TypeError: 'str' object in not callable" – phighter Jan 20 '15 at 23:19
  • 1
    Hmm... It sounds like you may have overwritten the builtin functions at some stage. Have you ever written assigned any of the functions to strings, e.g. `int = 'number'`? – Alex Riley Jan 20 '15 at 23:24
  • @ajcr Seems more likely he might've reassigned `str`. phighter can check by printing it out: `print(str)` should give `` and `print(int)` should give ``. – jpmc26 Jan 20 '15 at 23:27
  • @jpmc26 i tried that and i gave me and isthat normal – phighter Jan 20 '15 at 23:33
  • @phighter You must be using Python 3.x, even though your question is tagged with [tag:python-2.7]. And you did that in the same place where you're getting the error? That also has implications on this answer, since `map` seems to return a generator in Python 3.x – jpmc26 Jan 20 '15 at 23:35
  • @jpmc26 thanks i figured out the problem all working – phighter Jan 20 '15 at 23:40
  • in python 3.7.x – ali reza Jun 06 '19 at 04:51
6

You can convert the number into a string and then do a list comprehension -

>>>[int(digit) for digit in str(123456789)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Chris Wilding
  • 551
  • 3
  • 10
4

You can also do this without turning your number into a string like so:

def splitNum(n):
    if n < 10
        return [n]
    else:
        return splitNum(n // 10) + [n % 10]

This method uses recursion, but you can also do this without recursion

def splitNum(n):
    digits = []
    while n > 0:
        digits.insert(0, n % 10) # add ones digit to front of digits
        n = n // 10
    return digits

Both use the following facts:

  • x // 10, because of integer division, "chops off" the ones digit, ie 1234 // 10 is 123
  • x % 10 is that ones digit, ie 1234 % 10 is 4
JJW5432
  • 697
  • 8
  • 15
  • Note: this only works **without** `from __future__ import division`. If you have that enabled, you need to use the `//` operator for integer truncation. – jpmc26 Jan 20 '15 at 23:24
  • Apologies. Forgot about Python 3.x, where it applies as well. Interestingly, from other comments, it looks like 3.x is exactly what the OP is using. – jpmc26 Jan 20 '15 at 23:39
0

Use the inbuilt list function in Python in the following way:

a = 123456789
p = str(a)
li = list(p)
s = []
for e in li:
    a = int(e)
    s.append(a)
print s    

From the documentation:

list(iterable) -> new list initialized from iterable's items

EDIT:

Since the list() method returns a list containing only string elements, I have created an empty list s, and have then used a for loop to iterate through each string element, converted each of those elements to integer, and have then appended those elements inside the empty list s.

Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104