Let's say I've got a string mystr = "mylist[0][1]" and a list called mylist which contains many words. I want to print the second letter of a first word in my list (as my string suggests). How can I do this? When I try to use print(mystr) it obviously prints mylist[0][1] instead of my letter.
-
Does it have to be from a string? Could you use a list of indexes like `[0, 1]`? – Barmar Apr 01 '22 at 21:55
-
1`print(eval(mystr))` will do what you want, but `eval()` is dangerous because it can execute arbitrary code. – Barmar Apr 01 '22 at 21:55
-
See https://stackoverflow.com/questions/14692690/access-nested-dictionary-items-via-a-list-of-keys for a general way to do this with lists and dictionaries and a list of indexes/keys. – Barmar Apr 01 '22 at 21:58
-
What's the background of this question? How come you want to do this? – md2perpe Apr 01 '22 at 22:18
-
Well, at first I get a long string consisting of double indexes in form of long_str = "[1][2] [3][4] [5][6]" and also o list (let's call it mylist) with words which is supposed to work as a decryption key. Then I make a list of indexes out of long_str and for every double_index, I want to print mylist[double_index]. – rogolo Apr 01 '22 at 22:39
3 Answers
You can use the eval
function to interpret a string as code, but I don't recommend it.
print(eval(mystr))
There are many reasons why this is a bad idea:
It is very slow. The Python interpreter has to parse the string, analyze it and interpret it at this point in the program. If it's in a loop, it has to do all the work each time through the loop.
It may be a security risk for code injection. If any part of the string comes from an external input, an attacker could send it some undesirable code that your program would then execute.

- 6,002
- 1
- 28
- 38
-
1You don't need the concatenation. `print(eval(mystr))`. You're confusing it with `exec()`. – Barmar Apr 01 '22 at 21:57
-
@Barmar My version with eval("print...") would work too because print() is a valid Python expression and can be eval'ed, but I agree that your suggestion is simpler, edited. – Carlos A. Ibarra Apr 01 '22 at 22:04
-
I know it works. "don't need" means you're doing extra, unnecessary work. – Barmar Apr 01 '22 at 22:08
use something like this approach:
mystr = "mylist[0][1]"
splited = mystr.split('[')
i = int(splited[1].replace(']', ''))
j = int(splited[2].replace(']', ''))
print(mylist[i][j])

- 339
- 2
- 11
-
I suspect he wants something more general, so it can handle an arbitrary number of indexes. – Barmar Apr 01 '22 at 22:08
-
I've only got double indexes, so Parazok's method works perfectly fine. – rogolo Apr 01 '22 at 22:28
Instead of evaluating the string containing an object identifier you can try to access to it via the globals()
dictionary. This process is a bit tedious and can be done more rigorously, for example, using a regular expression.
mylist = [[1, 2, 3], [6, 7, 8]]
mystr = "mylist[0][1]"
key, coord1, coord2 = mystr.split('[')
# remove last character, ]
coord1 = int(coord1[:-1])
coord2 = int(coord2[:-1])
l = globals()[key][coord1][coord2]
print(l)

- 3,936
- 1
- 7
- 25