0

I want to use eval in the middle of the following python statement:

["a", "b"] + eval('[1,2].append(3)')

but because it does not return any value (it works in an "in place" manner), I cannot actually use it there and instead I receive an error of TypeError: can only concatenate list (not "NoneType") to list . Is there any way that I can fix this issue by getting return value out of eval in Python?

2
  • 4
    It's not that eval doesn't return anything here. It's that list.append() doesn't return anything. Commented Oct 13, 2021 at 16:23
  • 1
    @Axe319, you are right! Thank you! Commented Oct 13, 2021 at 16:25

1 Answer 1

3

You can use list concatenation instead of calling append(). It returns the new list.

["a", "b"] + eval('[1,2] + [3]')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.