0

I have a list, called checkerlist[] containing n items.

Here's how the values look (incrementally rises by 1):

check0 = result['ask'].iloc[0]
check1 = result['ask'].iloc[1]

and so on.

Essentially, I want to execute each line inside a for loop:

for ask in checkerlist:
           eval(ask)

However, I get this error:

  File "<string>", line 1
    check0 = result['ask'].iloc[0]
           ^
SyntaxError: invalid syntax

But if I were to run

check0 = result['ask'].iloc[0]
check0

I would get the desired result: 2.5

What am I doing wrong?

For reference- I hadn't originally had this code inside a list, just printed it out as is. However, adding it to a list has made it far more manageable (I'm still learning python). So any best practices you feel you can share will be well received.

  • of which structure are the "items"? How looks like check0? – cards Aug 15 '21 at 12:10
  • How have you defined checkerlist ? – cruisepandey Aug 15 '21 at 12:12
  • try without eval then... – cards Aug 15 '21 at 12:13
  • @cruisepandey checkerlist = [] is defined at the start of the code (didn't add it to this question). So yes, it's there. Trying type(checkerlist) returns list – MaximumPain Aug 15 '21 at 12:15
  • @cards without eval and using print(ask) returns the list position value as a string - check0 = result['ask'].iloc[0] – MaximumPain Aug 15 '21 at 12:16
  • @Kevin ok, but could you give an example of the structure of such string? is a statement? an expression? I guess here the point – cards Aug 15 '21 at 12:28
  • Isn't this a duplicate of [How to run code inside a loop](https://stackoverflow.com/questions/68790991/how-to-run-code-inside-a-loop-thats-not-code) where the consensus was this was a bad idea? Meaning, it's better to store values in a list or dictionary than storing them in variable names i.e. check0, check1, etc. – DarrylG Aug 15 '21 at 12:29
  • I took that information on board and did store as a list. It was a big help. But I still had trouble executing the code which was answered by nikichiko – MaximumPain Aug 15 '21 at 12:31
  • @Kevin--The comments from the other answers were to discourage generating a list with executable code since there are only a few special circumstances where this is needed. The normal Python solution is to use Patrick Artner's suggestion of: `results = [result['ask'].iloc[i] for i in range(n)]` for n values. Then `print(results)` which is faster and safer to run. If the result is a Dataframe then even simpler: `result['ask'].tolist()` for list of values. – DarrylG Aug 15 '21 at 12:38
  • @DarrylG you're right - I just tried Patrick's code there and it works great. I have revisited that question and updated on his comment. Thanks for following up on this. I was able to get this working using a list, however, this method works great and does indeed seem faster for my needs. – MaximumPain Aug 15 '21 at 13:24

1 Answers1

1

You're looking for exec, and not eval. Eval will evaluate some string value and return the value, no = in there. With exec you can actually run code as a string.

Something like:

for ask in checkerlist:
    exec(ask)
nikochiko
  • 183
  • 6
  • I had tried that and nothing displayed, so I added: for ask in checkerlist: print(exec(ask)) and it returns None as the answer – MaximumPain Aug 15 '21 at 12:18
  • Yes!! That is supposed to happen. Because you see, the assignment operation returns None. you can just as well do, `print(check0 = result['ask'].iloc[0])` and it will still give None. – nikochiko Aug 15 '21 at 12:19
  • 1
    If you want to print, you should add `print(checkN)` to each of the items in the checkerlist. So, each element should be something like `"check = result['ask'].iloc[0]; print(check0)"`. Then when you do `exec(ask)`, it will also print check0 – nikochiko Aug 15 '21 at 12:21
  • 1
    Boom! That worked thank you! I learned a lot from this, thanks for your patience. – MaximumPain Aug 15 '21 at 12:29