+ 2

[SOLVED] Can we insert code in input in Python, format it with f str, and then convert it back to what it really is?

Say I want to input lists in my code. A list may contain any data type, right. And say that my sample input is a list that I recognize as squares of counting numbers from 1 to 1000. Is there a way for me to input that as a list comprehension expression and then rid it of its strings and execute it? example: LstStr = '{}'. format(input("Enter list:")) Lst = ''. join(LstStr) I enter [i**2 for i in range(1,11)] and of course the result is itself. Is there a way for me to enter it just lije that yet get python to execute what's inside upon conversion, getting [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]? Is it doable, if at all, at beginner level? Or should we define a function for this? Should I think more and come back?

23rd Apr 2022, 2:49 PM
Korkunç el Gato
Korkunç el Gato - avatar
5 Answers
+ 2
LstStr = eval('{0}'. format(input("Enter list:"))) #Lst = ''. join(LstStr) print(LstStr) Like this?
23rd Apr 2022, 3:00 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 Oh no I just meant I lost my self control a bit, because of excitement. To lose it usually has a negative connotation, I guess, but it's great, you're great, eval is great, but me is not so great, is all I mean :-)
23rd Apr 2022, 3:23 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Jayakrishna🇮🇳 Woow ♥️♥️♥️♥️♥️♥️♥️♥️♥️ Thanks! 😲😲😸 E V A L. Thank you, :-) Sorry for losing it
23rd Apr 2022, 3:03 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Why sorry, what you lose? use of eval()? Anyways you're welcome.....
23rd Apr 2022, 3:08 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 My head, I guess lol, I refrain from pictured emojis normally but words weren't enough, because this is crazy, I was expecting people to say "no" I guess, and with mad speed, you knew what I was asking, and said "yes, you can do it" eval is kung fu. I hope I can stick my claws to this language, it seems so fun
23rd Apr 2022, 3:14 PM
Korkunç el Gato
Korkunç el Gato - avatar