13

So I am taking raw_input as an input for some list.

x= raw_input()

Where I input 1 2 3 4 How will I convert it into a list of integers if I am inputting only numbers?

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
user3481478
  • 387
  • 1
  • 3
  • 19

4 Answers4

23

Like this:

string_input = raw_input()
input_list = string_input.split() #splits the input string on spaces
# process string elements in the list and make them integers
input_list = [int(a) for a in input_list] 
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
  • @user3481478 You can accept the answer you find most useful. – Stefan van den Akker Apr 12 '14 at 08:27
  • Also, say you want to specify the length of the list, say 4 and take only 4 inputs into your list, this should help : `input_arr = []` `while len(input_arr) < 4: input_arr.append(raw_input("enter test arrays:\t"))` – geekidharsh Feb 17 '17 at 02:14
10

list = map(int,raw_input().split())

We are using a higher order function map to get a list of integers.

The Process
  • 5,913
  • 3
  • 30
  • 41
Perseus14
  • 467
  • 1
  • 6
  • 19
  • this works only for numbers or integers, but a list may also have combined datatypes as elements!!!! viz: list_new=['aaa','bbb',23,45,'ccc'] in such a case how do we split them when read as string... – frp farhan May 06 '17 at 16:22
  • You substitute 'int' with the required datatype. – Perseus14 May 06 '17 at 16:26
  • i have a mixed datatype as u can see in the above comment.....hence i can't substitute any static value over there.... – frp farhan May 06 '17 at 16:27
  • A custom function can usually be written for specific cases. I think it would be better if you post this as a separate question – Perseus14 May 06 '17 at 16:40
  • posted a separate question....thnx for the encouragement @perseus.... http://stackoverflow.com/questions/43822895/how-to-separate-a-mixed-datatype-string-taken-as-input-into-list – frp farhan May 06 '17 at 16:41
3

You can do the following using eval:

lst = raw_input('Enter your list: ')
lst = eval(lst)
print lst

This runs as:

>>> lst = raw_input('Enter your list: ')
Enter your list: [1, 5, 2, 4]
>>> lst = eval(lst)
>>> print lst
[1, 5, 2, 4]
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
1

Here are some examples and brief explanation for Inputting Lists from users:

You may often need code that reads data from the console into a list. You can enter one data item per line and append it to a list in a loop. For example, the following code reads ten numbers one per line into a list.

lst1 = [] # Create an empty list
print("Enter 10 Numbers: ")
for i in range(10):
   lst1.append(eval(input()))

print(lst1)

Sometimes it is more convenient to enter the data in one line separated by spaces. You can use the string’s split method to extract data from a line of input. For example, the following code reads ten numbers separated by spaces from one line into a list.

# Read numbers as a string from the console
s = input("Enter 10 numbers separated by spaces from one line: ")
items = s.split() # Extract items from the string
lst2 = [eval(x) for x in items] # Convert items to numbers

print(lst2)

Invoking input() reads a string. Using s.split() extracts the items delimited by spaces from string s and returns items in a list. The last line creates a list of numbers by converting the items into numbers.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Jocelyn
  • 396
  • 3
  • 5