Search code examples
pythonlistpython-2.7raw-input

How to make a list from a raw_input in python?


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?


Solution

  • 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]