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

4

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?

Ludlow answered 12/4, 2014 at 5:5 Comment(2)
map(int, raw_input.split())Anetteaneurin
@StevenRumbalski map(int, raw_input().split()) ?Emanate
E
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] 
Emanate answered 12/4, 2014 at 5:6 Comment(2)
@Ludlow You can accept the answer you find most useful.Ligniform
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"))Midcourse
E
10

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

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

Eun answered 15/3, 2016 at 19:0 Comment(5)
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...Ichang
You substitute 'int' with the required datatype.Eun
i have a mixed datatype as u can see in the above comment.....hence i can't substitute any static value over there....Ichang
A custom function can usually be written for specific cases. I think it would be better if you post this as a separate questionEun
posted a separate question....thnx for the encouragement @perseus.... #43823395Ichang
E
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]
>>> 
Entasis answered 1/5, 2014 at 3:40 Comment(0)
U
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.

Usquebaugh answered 12/4, 2014 at 8:19 Comment(2)
maybe include the link where you found this text and the examples?Ligniform
Introduction to Programming Using Python by Y. Daniel Liang page 313Usquebaugh

© 2022 - 2024 — McMap. All rights reserved.