How do i print an array in one line in python?

I'm new to python and I'm trying to scan multiple numbers separated by spaces (let's assume '1 2 3' as an example) in a single line and add it to a list of int. I did it by using:

#gets the string 
string = input('Input numbers: ') 
#converts the string into an array of int, excluding the whitespaces
array = [int(s) for s in string.split()] 

Apparently it works, since when I type in '1 2 3' and do a print(array) the output is:

[1, 2, 3]

But I want to print it in a single line without the brackets, and with a space in between the numbers, like this:

1 2 3

I've tried doing:

for i in array:
    print(array[i], end=" ")

But I get an error:

2 3 Traceback (most recent call last):

print(array[i], end=" ")

IndexError: list index out of range

How can I print the list of ints (assuming my first two lines of code are right) in a single line, and without the brackets and commas?

asked Jun 4, 2016 at 0:30

How do i print an array in one line in python?

1

Yes that is possible in Python 3, just use * before the variable like:

print(*list)

This will print the list separated by spaces.

(where * is the unpacking operator that turns a list into positional arguments, print(*[1,2,3]) is the same as print(1,2,3), see also What does the star operator mean, in a function call?)

How do i print an array in one line in python?

user2314737

25.2k18 gold badges94 silver badges106 bronze badges

answered Aug 19, 2018 at 5:35

How do i print an array in one line in python?

1

You want to say

for i in array:
    print(i, end=" ")

The syntax i in array iterates over each member of the list. So, array[i] was trying to access array[1], array[2], and array[3], but the last of these is out of bounds (array has indices 0, 1, and 2).

You can get the same effect with print(" ".join(map(str,array))).

answered Jun 4, 2016 at 0:38

Nick MatteoNick Matteo

4,30721 silver badges31 bronze badges

0

these will both work in Python 2.7 and Python 3.x:

>>> l = [1, 2, 3]
>>> print(' '.join(str(x) for x in l))
1 2 3
>>> print(' '.join(map(str, l)))
1 2 3

btw, array is a reserved word in Python.

answered Jun 4, 2016 at 0:56

How do i print an array in one line in python?

Corey GoldbergCorey Goldberg

57.1k27 gold badges123 silver badges141 bronze badges

2

Try using join on a str conversion of your ints:

print(' '.join(str(x) for x in array))

For python 3.7

How do i print an array in one line in python?

answered Jun 4, 2016 at 0:38

EoinSEoinS

5,1751 gold badge18 silver badges31 bronze badges

2

You have multiple options, each with different general use cases.

The first would be to use a for loop, as you described, but in the following way.

for value in array:
    print(value, end=' ')

You could also use str.join for a simple, readable one-liner using comprehension. This method would be good for storing this value to a variable.

print(' '.join(str(value) for value in array))

My favorite method, however, would be to pass array as *args, with a sep of ' '. Note, however, that this method will only produce a printed output, not a value that may be stored to a variable.

print(*array, sep=' ')

answered Jun 4, 2016 at 1:45

How do i print an array in one line in python?

2Cubed2Cubed

3,2216 gold badges22 silver badges40 bronze badges

1

If you write

a = [1, 2, 3, 4, 5]
print(*a, sep = ',')

You get this output: 1,2,3,4,5

How do i print an array in one line in python?

answered Nov 30, 2019 at 13:40

How do i print an array in one line in python?

# Print In One Line Python

print('Enter Value')

n = int(input())

print(*range(1, n+1), sep="")

How do i print an array in one line in python?

Suraj Rao

29.1k11 gold badges95 silver badges100 bronze badges

answered Mar 4, 2019 at 14:57

How do i print an array in one line in python?

1

lstofGroups=[1,2,3,4,5,6,7,8,9,10]

print(*lstofGroups, sep = ',')

don't forget to put * before the List

answered Apr 7 at 0:57

For python 2.7 another trick is:

arr = [1,2,3]
for num in arr:
  print num,
# will print 1 2 3

answered Feb 7, 2019 at 4:30

How do i print an array in one line in python?

elad silverelad silver

8,5294 gold badges41 silver badges63 bronze badges

you can use more elements "end" in print:

for iValue in arr:
   print(iValue, end = ", ");

answered Sep 25, 2020 at 10:03

How do i print an array in one line in python?

Maybe this code will help you.

>>> def sort(lists):
...     lists.sort()
...     return lists
...
>>> datalist = [6,3,4,1,3,2,9]
>>> print(*sort(datalist), end=" ")
1 2 3 3 4 6 9

you can use an empty list variable to collect the user input, with method append(). and if you want to print list in one line you can use print(*list)

answered Jul 3, 2020 at 3:53

How do i print an array in one line in python?

montimonti

1821 silver badge8 bronze badges

How do I print an array of elements in one line?

Print Array in One Line with Java Streams Arrays. toString() and Arrays. toDeepString() just print the contents in a fixed manner and were added as convenience methods that remove the need for you to create a manual for loop.

How do you enter an array in one line in Python?

Answer: Use map() function along with input and split function. Using int to Read an array of integers only.

How do you print all values on one line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.

How do I print all values on one line?

Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.