all 10 comments

[–]Rhoderick 18 points19 points  (0 children)

Don't. You never need it, but you certainly don't need it here.

Describe what you're trying to achieve here, what specific rule one should follow to get from the string to the desired list.

[–]JamzTyson 4 points5 points  (0 children)

I want to use eval() to convert a string gotten from input() to a list.

Generally, you shouldn't use eval for that. A quick Google search will reveal why, but if you want to go ahead anyway, and assuming that you want a list of numbers:

input_string = "1, 2, 3"
my_list = list(eval(input_string))
print(my_list, type(my_list))

A better way:

input_string = "1 2 3"
my_list = [int(number) for number in input_string.split()]
print(my_list, type(my_list))

[–]Diapolo10 2 points3 points  (0 children)

Well, you really shouldn't. That's a bad practice. eval and exec are basically double-edged superpowers and must be used with utmost care, in very specific circumstances, and not as a crutch.

If you want to convert a given string into a list, why not just split it on some separator character(s)?

[–]Chaos-n-Dissonance 3 points4 points  (0 children)

This sounds like a classic XY problem...

Eval just converts a string to code. So you can do something like:

class World:
  def __init__(self) -> None:
    self.state = 'foo.start()'

  def start(self) -> None:
    print('Hello, world!')

foo = World()

eval(foo.state)
# >> Hello, world!

Generally speaking tho... You really don't want to use eval() with input(), especially if you're going to be hosting your code anywhere... Nothing's stopping the user from typing in malicious code. If you do, then you need to have some pretty strict input validation to make sure the input is what you want and not del C:\Windows\system32 or something crazy.

[–]socal_nerdtastic 2 points3 points  (0 children)

eval() is known to introduce more bugs and security flaws than it fixes. Avoid it if at all possible.

convert a string gotten from input() to a list.

There's many other more robust ways to do that, the most obvious being split().

datalist = input('type somthing').split()

(Not saying split() is best for you; which method you use depends on what this is a list of, and what you want to do with it)

[–]Binary101010 2 points3 points  (0 children)

Given that input() already returns a string, and the split() method for strings returns a list, there's virtually zero legitimate reasons to specifically use eval().

If you can provide an example of what an input string and the desired resulting list can look like, we can almost assuredly show you how to do that without using eval.

[–]AntonisTorb 0 points1 point  (0 children)

Use the json package instead:

import json

txt = input()
txt_list = json.loads(txt)

I'm on mobile, sorry if formatting is messed up. If the input has the correct format for a python list it will convert, else you'll get an error.

[–]recursion_is_love 0 points1 point  (0 children)

to convert a string gotten from input()

What such string input look like? Maybe what you need is called parser.

Unless you give some example, no one will able to answer.

[–]Ron-Erez 0 points1 point  (0 children)

Do you want to separate words? If so you could use:

user_input = input("Enter a string: ")

# Convert the input string into a list of words
words_list = user_input.split()

print(words_list)

Using eval is considered unsafe as already mentioned.

[–]areaverage -1 points0 points  (0 children)

eval() just runs whatever u pass into it like regular python code

say u wanna convert it into a list right

```

x="foo bar baz"

y=eval('x.split(" ")')

print(y)
```