0

I'm trying to get a list from the input function

programme = input('giving list')
#giving a list ['1','2','2']

print(programme)
noob = list(programme)
print(type(noob))
print(noob)

Here is the terminal answer

giving list['1','2','2']

['1','2','2']
<class 'list'>
['[', "'", '1', "'", ',', "'", '2', "'", ',', "'", '2', "'", ']']

is it possible to recover 'noob' variable like :

noob = ['1','2','2'] 

instead of noob = ['[', "'", '1', "'", ',', "'", '2', "'", ',', "'", '2', "'", ']']

4
  • User should just type: 1,2,2. print(programme.split(',')) will be ['1','2','2'] Commented Nov 27, 2019 at 19:28
  • 4
    There is "ast.literal_eval()" for this. Commented Nov 27, 2019 at 19:29
  • Is there any specific format you would like to have the input? for example 1,2,3,4,5 or do you want to have multiple inputs each of them appended to a list? Commented Nov 27, 2019 at 19:29
  • micheal Bustcher you gaveme the correct answer. how can i make you as problem solver ? Commented Nov 27, 2019 at 19:33

2 Answers 2

1

This should do what you want.

import ast

def get_list_input(input_string):
    return ast.literal_eval(input(input_string))
Sign up to request clarification or add additional context in comments.

Comments

0

python input is always in string format. So you need to get entries on how DarrylG suggested. Ask users to enter data in comma-separated or colon-separated.

You can also create a loop and ask user to enter the entries one by one. Append this data to a list. Keep a key to exit the input for loop. Like Enter "exit_this" to exit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.