11

I want to read an array of integers from single line of input in python3. For example: Read this array to a variable/list

1 3 5 7 9

What I have tried

  1. arr = input.split(' ') But this does not convert them to integers. It creates array of strings

  2. arr = input.split(' ')

    for i,val in enumerate(arr): arr[i] = int(val)

2nd one is working for me. But I am looking for an elegant(Single line) solution.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168

5 Answers5

39

Use map:

arr = list(map(int, input().split()))

Just adding, in Python 2.x you don't need the to call list(), since map() already returns a list, but in Python 3.x "many processes that iterate over iterables return iterators themselves".

This input must be added with () i.e. parenthesis pairs to encounter the error. This works for both 3.x and 2.x Python

Community
  • 1
  • 1
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
  • Thanks..It works and is faster solution than 2nd one proposed by me. You know, I tried `map(int, input.split(' ')` but It was returning map not list. Your answer solved this one too. Thanks!! – Sachin Jain Aug 20 '13 at 10:58
6

Edit: After using Python for almost 4 years, just stumbled back on this answer and realized that the accepted answer is a much better solution.

Same can be achieved using list comprehensions:
Here is example on ideone:

arr = [int(i) for i in input().split()]

If you are using Python 2, you should use raw_input() instead.

harmands
  • 1,082
  • 9
  • 24
  • @crisron `>>> a = [int(i) for i in input().split()]` `1 2 3 4 5 6` `>>> a` `[1, 2, 3, 4, 5, 6]` [Click here for ideone example](https://ideone.com/VbwjrG) – harmands Dec 20 '15 at 12:10
  • I don't understand why this doesn't work for me. On running this code, I am getting the prompt for input. I am giving input like this: `1 2 3 4 5`. It says `SyntaxError`. – crisron Dec 20 '15 at 16:49
  • 1
    @crisron are you using python2? if that is the case use the following:- `a = [int(i) for i in raw_input().split()]` that means use `raw_input()` in place if `input()` – harmands Dec 20 '15 at 17:54
2

You can get a good reference from the following program

# The following command can take n number of inputs 
n,k=map(int, input().split(' '))
a=list(map(int,input().split(' ')))
count=0
for each in a:
    if each >= a[k-1] and each !=0:
        count+=1
print(count)
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
  • 1
    Code only answers are normally discouraged. Do you think you could add a little description of how this works? – Jason Sep 11 '19 at 15:26
0

You can try this below code that takes an input from user and reads it as array and not list.

from array import *
a = array('i',(int(i) for i in input('Enter Number:').split()))
print(type(a))
print(a)

IN addition , if you wish to convert it to a list:

b = a.tolist()
print(type(b))  
print(b)
Anil Sharma
  • 53
  • 1
  • 6
0
Just adding in :

1) If you want to take input on different lines use this:
    
n = int(input())
arr = [int(input()) for i in range(n)]

    for e.g 

    n = 5
    arr
    1
    2
    3
    4
    5

2) If you want input on the same line use `map`:

    As mentioned in above comment:

It returns a map iterator so we are type casting it to list

    n = int(input())
    arr = list(map(int, input().split()))


for e.g
n = 5
arr = [1 2 3 4 5]
Navin Ojha
  • 29
  • 6