0

I have one little problem with my python knowledge (novice  ). The thing I'm trying to do is input list of strings to variable and then I'll need to do some for looping over the list and return few strings from list.

The problem is that I don't know how to input a list to variable.

Here's a code how I tried:

x=[]
x=(input('Please enter a list of strings: '))

...and then I entered ['car', 'house', 't-shirt', 'bicycle', 'images'] assuming that x will be a variable with list of strings but it's not.

Please help. Thx!

5
  • 3
    but what is it? try print repr(x) in order to see what's in x after input. Oh, and you don't have to declare lists in python. x=[] will be overridden by input('Please enter a list of strings: ') Commented Nov 16, 2012 at 12:09
  • I guess that all list is saved as a one string. When I do for i in the x: print(i) it returns whole imported list string by string Commented Nov 16, 2012 at 12:12
  • 1
    So, if it's crucial that user enters list in python format, you can use x = eval(x) but this is dangerous because malicious data can be entered. I'd propose you to make user input words through space or newline like this car house t-shirt and then just use x = x.split() after doing input Commented Nov 16, 2012 at 12:17
  • thanks Ashwini, I'll try this idea with spaces and then .split() Commented Nov 16, 2012 at 12:20
  • 2
    @alex_jordan for this application it would be safer to use ast.literal_eval() than to use eval() Commented Nov 16, 2012 at 12:26

4 Answers 4

3

I'm going to assume you're using Python 3 (if this is Python 2 code, you have a problem).

From the documentation:

input(...): ...The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

So, input() returns a string. Not a list or anything.

Of course, you can split() the string on commas, and strip() characters like '[' or ']' from the end.

Sign up to request clarification or add additional context in comments.

3 Comments

Wrong. input()'s help tells you: Equivalent to eval(raw_input(prompt)). So it evaluates what is typed as a Python expression.
@Alfe I downvote your downvote. You're talking Python 2 while Evert specified Python 3. stackoverflow.com/questions/4915361/…
Oh, I didn't notice the restriction of Python 3 in the headline. Sorry for that. Maybe the difference between P2 and P3 is really what the asker stumbled over.
2

I am assuming you are using python 3.x. If you are trying to input a list, using input I think the data will be treated as string even though you type in ['car', 'house', 't-shirt', 'bicycle', 'images']

You can try this to convert the data to a list:

str_x = input('Please enter a list of strings: '))
x = [s.strip() for s in str_x[1:-1].split(',')]   # String to List conversion

Comments

0

Here's another way to do it:

>>> x=input('Please enter a list of strings: ').split(sep=', ')
Please enter a list of strings: hi, there, stack, overflow
>>> print(x)
['hi', 'there', 'stack', 'overflow']

This way you're letting input() do the work as it takes the input as a string as default. .split() returns a list, but you could always use

x=list(input(...))

just in case future versions decide to return a tuple instead.

You could also change the sep, i.e. you might just use a comma, instead of a comma and space, just depends on your own style.

Comments

0

In Python 2, this will work

x=raw_input('Please enter a list of strings: ').split()
print x

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.