I am trying to print out all the elements of a list one by one, like this
a=[1,2,3]
print(i for i in a)
how do i print out
1
2
3
I am trying to print out all the elements of a list one by one, like this
a=[1,2,3]
print(i for i in a)
how do i print out
1
2
3
You want to use a for-loop here, not a generator expression:
>>> a = [1, 2, 3]
>>> for i in a:
... print(i)
...
1
2
3
>>> # As a one-liner
>>> for i in a: print(i)
...
1
2
3
>>>
Or, if you want to get fancy, you can use just print
and argument unpacking:
>>> a = [1, 2, 3]
>>> # This is the same as doing: print(1, 2, 3, sep="\n")
>>> print(*a, sep="\n")
1
2
3
>>>
Here are two ways you can accomplish what you want.
a = [1, 2, 3]
for a_val in a:
print a_val
for i in range(0, len(a)):
print a[i]