I have a list with list operations, For example ["insert", "append"], For now I am doing like if list says insert then I am calling .insert.
Check below code
In [31]: list_ops = [ "insert" , "append" ]
In [32]: list1 = [ 1 , 3 , 5 ]
In [33]: if list_ops[0] == "insert" :
...: list1.insert(3, 7)
...:
In [34]: list1
Out[34]: [1, 3, 5, 7]
In [35]: list1.list_ops[0](4, 9 )
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-5db0dcceeac1> in <module>
----> 1 list1.list_ops[0](4, 9 )
AttributeError: 'list' object has no attribute 'list_ops'
In [36]:
How can I choose the operation from list_ops itself instead of doing if-else matching.
Thank you.