Get a list of numbers as input from the user

488,534

Solution 1

In Python 3.x, use this.

a = [int(x) for x in input().split()]

Example

>>> a = [int(x) for x in input().split()]
3 4 5
>>> a
[3, 4, 5]
>>> 

Solution 2

It is much easier to parse a list of numbers separated by spaces rather than trying to parse Python syntax:

Python 3:

s = input()
numbers = list(map(int, s.split()))

Python 2:

s = raw_input()
numbers = map(int, s.split())

Solution 3

eval(a_string) evaluates a string as Python code. Obviously this is not particularly safe. You can get safer (more restricted) evaluation by using the literal_eval function from the ast module.

raw_input() is called that in Python 2.x because it gets raw, not "interpreted" input. input() interprets the input, i.e. is equivalent to eval(raw_input()).

In Python 3.x, input() does what raw_input() used to do, and you must evaluate the contents manually if that's what you want (i.e. eval(input())).

Solution 4

You can use .split()

numbers = raw_input().split(",")
print len(numbers)

This will still give you strings, but it will be a list of strings.

If you need to map them to a type, use list comprehension:

numbers = [int(n, 10) for n in raw_input().split(",")]
print len(numbers)

If you want to be able to enter in any Python type and have it mapped automatically and you trust your users IMPLICITLY then you can use eval

Solution 5

Another way could be to use the for-loop for this one. Let's say you want user to input 10 numbers into a list named "memo"

memo=[] 
for i in range (10):
    x=int(input("enter no. \n")) 
    memo.insert(i,x)
    i+=1
print(memo) 
Share:
488,534
Underyx
Author by

Underyx

I have approximate knowledge of many things.

Updated on July 08, 2022

Comments

  • Underyx
    Underyx almost 2 years

    I tried to use input (Py3) /raw_input() (Py2) to get a list of numbers, however with the code

    numbers = input()
    print(len(numbers))
    

    the input [1,2,3] and 1 2 3 gives a result of 7 and 5 respectively – it seems to interpret the input as if it were a string. Is there any direct way to make a list out of it? Maybe I could use re.findall to extract the integers, but if possible, I would prefer to use a more Pythonic solution.

  • AJ Dhaliwal
    AJ Dhaliwal about 8 years
    After python 2.7 raw_input() was renamed to input(). Stack overflow answer
  • Underyx
    Underyx almost 8 years
    I don't think this is simpler than the accepted answer.
  • greentec
    greentec about 7 years
  • frp farhan
    frp farhan about 7 years
    what if my input is having mixed datatype string and integer then how can i split them and convert to list. input: 'aaa' 3 45 554 'bbb' 34 'ccc' i added the contents seperated by space!!
  • Sven Marnach
    Sven Marnach about 7 years
    @FarhanPatel That's an unrelated question, so I suggest asking a new question. Start with looping over s.split() or shlex.split(s) if you want to allow spaces inside quoted strings.
  • frp farhan
    frp farhan about 7 years
    posting it as a new question, thnx for the encouragement, was scary it might not be marked as duplicate!! stackoverflow.com/questions/43822895/…
  • Stevoisiak
    Stevoisiak over 6 years
    Can this accept different data types other than int? i.e. Can I replace int(x) with string(x)
  • Vikhyat Agarwal
    Vikhyat Agarwal over 6 years
    @StevenVascellaro Yes although the expression would be str(x) instead of string(x) since str is python's way of saying string
  • Bernhard Barker
    Bernhard Barker about 6 years
    @DhirajBarnwal If you want to know what's going on there: input() to read input + split() to split the input on spaces + [f(x) for x in iter] to loop over each of those + int to turn each into an int. If you want to know how one can come up with it: it's mostly just figuring out what you want to do and which pieces you need to put together to achieve that.
  • Rishav
    Rishav about 5 years
    I would have +1 if you had an extra 2 lines of explanation which @Dukeling did in the comments.
  • Tomerikoo
    Tomerikoo about 3 years
    This takes each number as a separate input, on a separate line. The question implies a single line of input with one call to input...
  • Tomerikoo
    Tomerikoo about 3 years
    What if I want to pass 1, 11, 111or maybe 11, 1, 111? In your program they will all just be 1, 1, 1, 1, 1, 1... Bottom line, this only works for single digit numbers which is pretty restrictive...