-1

I want to have a user input a number, and that number is turned into a list named digits. I need to be able to do this without any seperators like space or comma...

number=int(input())
digits=[]

#

print (digits)

Example:
User input: 808
Code output: [8,0,8]

  • 3
    You can use call `list()` on a string to turn it into a list of characters. E.g `list('808')` – larsks Oct 30 '21 at 22:35
  • 1
    This would be easier if you kept the input as a string instead of immediately converting it to an integer. Why are you doing that? – John Gordon Oct 30 '21 at 22:45

2 Answers2

2

Try this:

list(map(int, input()))
Jab
  • 26,853
  • 21
  • 75
  • 114
2

The return from input() is a string which is already iterable by-character!

Assuming all the members of the input are really numbers, you can iterate over it, converting each to an int() as you go in a List Comprehension

digits = [int(x) for x in input()]

this is a shorthand for a loop like

digits = []
source_string = input("enter digits: ")
for value in list(source_string):
    digits.append(int(value))

Some error-handling (ValueError, TypeError) may be wanted to nicely handle non-int inputs the user may provide

ti7
  • 16,375
  • 6
  • 40
  • 68
  • `for value in list(source_string):` why did you use `list` on `source_string`? – juanpa.arrivillaga Oct 30 '21 at 22:53
  • ah, it's my hope this makes it clearer both what will be iterated over and that simply `list()` on a string makes a list of the characters; there's no practical reason to do more work than the comprehension does – ti7 Oct 30 '21 at 23:06
  • No, it absolutely doesn't make it clear, it *confused* because you don't do it in comprehension, and because it does absolutely nothing useful. – juanpa.arrivillaga Oct 31 '21 at 00:07
  • @juanpa.arrivillaga you are an _expert_, while I am illustrating two distinct things in attempt to provoke that very question in a small space; why is it needed here and not in the other position (it of course isn't!) – ti7 Oct 31 '21 at 17:11