Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a list from a raw_input in python? [duplicate]

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?

like image 883
user3481478 Avatar asked Apr 12 '14 05:04

user3481478


People also ask

What does the python raw_input () function do?

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function.

How do you copy a input in python?

The copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set.

What is raw_input () in python give an example?

a = input() will take the user input and put it in the correct type. Eg: if user types 5 then the value in a is integer 5. a = raw_input() will take the user input and put it as a string. Eg: if user types 5 then the value in a is string '5' and not an integer.

What is the difference between raw_input () and input () in python?

Basically, the difference between raw_input and input is that the return type of raw_input is always string, while the return type of input need not be string only. Python will judge as to what data type will it fit the best.


2 Answers

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] 
like image 97
shaktimaan Avatar answered Oct 12 '22 14:10

shaktimaan


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

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

like image 26
Perseus14 Avatar answered Oct 12 '22 16:10

Perseus14