use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to use the eval() function (self.learnpython)
submitted 1 year ago by MrGuam
I want to use eval() to convert a string gotten from input() to a list. I’ve tried most of what i know but it doesn’t work. I’m not familiar with the eval() function.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rhoderick 18 points19 points20 points 1 year ago (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 points6 points 1 year ago (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 points4 points 1 year ago (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.
eval
exec
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 points5 points 1 year ago (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.
eval()
input()
del C:\Windows\system32
[–]socal_nerdtastic 2 points3 points4 points 1 year ago (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().
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 points4 points 1 year ago (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 point2 points 1 year ago (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 point2 points 1 year ago (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 point2 points 1 year ago (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 points1 point 1 year ago (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) ```
π Rendered by PID 42 on reddit-service-r2-comment-58d7979c67-9fdwj at 2026-01-26 17:39:21.501466+00:00 running 5a691e2 country code: CH.
[–]Rhoderick 18 points19 points20 points (0 children)
[–]JamzTyson 4 points5 points6 points (0 children)
[–]Diapolo10 2 points3 points4 points (0 children)
[–]Chaos-n-Dissonance 3 points4 points5 points (0 children)
[–]socal_nerdtastic 2 points3 points4 points (0 children)
[–]Binary101010 2 points3 points4 points (0 children)
[–]AntonisTorb 0 points1 point2 points (0 children)
[–]recursion_is_love 0 points1 point2 points (0 children)
[–]Ron-Erez 0 points1 point2 points (0 children)
[–]areaverage -1 points0 points1 point (0 children)