0

I'm currently new in python and i'm trying to split digits into list. Now i want to show all that digits one by one in single row after splitting.

Suppose, I give input to my program like this -> 123456

I want to achieve my output like this

1
2
3
4
5
6

So, i tried this codes:

myList = input("\nEnter: ")

myElements = [int(i) for i in str(myList)]
print(myElements)

So, i'm able to get output like this:

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

QUESTION

But i want to get that digits in every single new row, like this:

1
2
3
4
5
6

I tried to use print(myElements[0]) but problem is, I have given a choice to insert, So how it is possible? Please help me..

HELP WOULD BE APPRECIATED!~

stackoverflow
  • 197
  • 1
  • 1
  • 4

4 Answers4

0

So you have your list. Now you need to iterate over the elements in your list and print them out.

for i in mylist:
    print i

Where i is an arbitrary variable (it could be anything, we use i as a convention) and mylist is the variable name of the list you generated. i takes the takes the first element from the list starting at index 0 to the end of your list. To simple print what your elements were, you print i, since i is equal an element in your list every time it loops.

To print a list of 6 numbers starting at one you can use

for i in range(1, 7):
    print i

if using python 3.

or if using python 2 use.

for i in xrange(1, 7):
    print i

If using python 2 range() will work, but I suggest you get into the habit of using xrange() if you're working with python2. Notice range starts at 1 and ends at 7. That means that it will start counting from 1 and end at 6. The difference between range and range can be read here What is the difference between range and xrange functions in Python 2.X?

Community
  • 1
  • 1
reticentroot
  • 3,612
  • 2
  • 22
  • 39
0
myList = input("\nEnter: ")

myElements = [int(i) for i in str(myList)]
for i in range(len(myElements)):
    print myElements[i]

Try this

Ajay
  • 5,267
  • 2
  • 23
  • 30
0

Try this:

print(str(myElements)[1:-1].replace(', ','\n'))
mmachine
  • 896
  • 6
  • 10
0

you have to use loops for this kind of results ,, trying to perform this by list comprehension will not work

[ print x for x in str(12345)]

because here , in list comprehension we try to assign print x (python2.7) for as a list , which is wrong because print x is not a variable instead it is a side effect which put the value of x to appear in screen., finally you have to loop over the list to get the result,

for i in list(12345):
    print i
lazarus
  • 677
  • 1
  • 13
  • 27