1

Simple problem.

I have a number (integer):

mynumber = 1239

I want to convert it into a list of the separate integers like [1,2,3,9]:

numbersplit =  [int(x) for x in mynumber]

But I get the error:

TypeError: 'int' object is not iterable

Why doesn't this work? I'm just making sure the ints are actually ints.

However it does work when I wrap it in str..?

[int(x) for x in str(mynumber)]

[1, 2, 3, 9, 8, 9, 8, 7, 2, 3, 9, 4, 7, 8]

Do lists need to be converted into a string before I can do this?

SCool
  • 3,104
  • 4
  • 21
  • 49
  • yes.. you have to convert to string. As string internally work like a list. But integer does not work like list – Harsha Biyani Apr 20 '20 at 09:39
  • Does this answer your question? [Convert a number to a list of integers](https://stackoverflow.com/questions/780390/convert-a-number-to-a-list-of-integers) – kederrac Apr 20 '20 at 09:49
  • Does this answer your question? [Splitting integer in Python?](https://stackoverflow.com/questions/1906717/splitting-integer-in-python) – dspencer Apr 20 '20 at 10:04

7 Answers7

1

You cannot iterate on an int, but you can do it on a string.

One approach is to convert it to a string:

mynumber = 1239
number_str = str(mynumber)

Then you can create your list, converting it back to int

[int(x) for x in number_str]
Jonath P
  • 519
  • 5
  • 16
1

Try this :

[int(i) for i in str(mynumber)]
Django0602
  • 797
  • 7
  • 26
1

When you try to do for x in mynumber you are assuming that mynumber is an iterable but it is actually an integer. You need to type cast it into a str, then it could be considered as an iterable. You can do for x in str(mynumber) - that way it would give you each character in each iteration and you can then convert each char into integer. Try this :

numbersplit =  [int(x) for x in str(mynumber)]
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
0

Do lists need to be converted into a string before I can do this?

No. You can iterate over a list just fine.

Iterating over a string gives you the individual letters, while you cannot iterate over an int.

The reason you can iterate over a string is because a Python string is actually a sequence type.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
0

for iterates on an iterable data such as list, string, dictionaries etc. int object is not iterable data type ans we cannot access int digits separately as we can access strings or list with the help of indexes.

0

Or use:

numbersplit = list(map(int, str(mynumber)))

Integers aren't iterable, to know whether an object is iterable, try using:

>>> iter(1)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    iter(1)
TypeError: 'int' object is not iterable
>>> iter('a')
<str_iterator object at 0x000000CE599A40B8>
>>> 

If it throws an error it means it isn't iterable.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

an integer is not iterable, you can iterate over his digits after you covert him to a string

list(map(int, str(mynumber)))
kederrac
  • 16,819
  • 6
  • 32
  • 55