Home > Net >  how to take list as input in single line python
how to take list as input in single line python

Time:01-03

how to take list as a input in python in a single line.

enter image description hereI tried following thing but it didn't worked

CodePudding user response:

You need to take input separated by space:

input_string = input("Enter a list element separated by space ")
lst  = input_string.split()

You can also take input separated by any other character as well.

CodePudding user response:

You can do so by accepting the numbers on a single line separated by spaces and then split it to get all the numbers as a list, then map it to an integer value to produce the required integer list.

numList = list(map(int, input("Enter a list of numbers separated by spaces: ").split()))
print(numList)

CodePudding user response:

You can make use of eval function.

_list = eval(input("enter the list:"))

When prompted you pass the list as follow: [1, 2, 3]

Your _list variable will be a list structure containing 1, 2 and 3 as elements.

Edit: this, of course, does not guarantee that only lists will be accepted on the input, so have this in mind.

CodePudding user response:

list1=input("data with spaces: ").split(" ") list2 = input("data with commas: ")split(",")

  • Related