Skip to content
Advertisement

Using eval in the middle of a python statement

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?

Advertisement

Answer

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

["a", "b"] + eval('[1,2] + [3]')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement