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!

JS.
  • 14,781
  • 13
  • 63
  • 75
Tomislav_St
  • 3
  • 1
  • 3
  • 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: ')` – alex_jordan Nov 16 '12 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 – Tomislav_St Nov 16 '12 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 – alex_jordan Nov 16 '12 at 12:17
  • thanks Ashwini, I'll try this idea with spaces and then .split() – Tomislav_St Nov 16 '12 at 12:20
  • 2
    @alex_jordan for this application it would be safer to use `ast.literal_eval()` than to use `eval()` – Matt Nov 16 '12 at 12:26

4 Answers4

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.

  • Wrong. `input()`'s help tells you: Equivalent to eval(raw_input(prompt)). So it evaluates what is typed as a Python expression. – Alfe Nov 16 '12 at 12:20
  • 2
    @Alfe I downvote your downvote. You're talking Python 2 while Evert specified Python 3. http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – Kos Nov 16 '12 at 12:20
  • 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. – Alfe Nov 16 '12 at 12:23
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
Pulimon
  • 1,776
  • 4
  • 31
  • 45
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.

John Durrans
  • 156
  • 5
0

In Python 2, this will work

x=raw_input('Please enter a list of strings: ').split()
print x
Kai Iyer
  • 133
  • 1
  • 5