Given a list containing numbers and arithmetic operators, the task is to solve the list.
Input: lst = [2, '+', 22, '+', 55, '+', 4] Output: 83 Input: lst = [12, '+', 8, '-', 5] Output: 15
Below are some ways to achieve the above tasks.
Method #1: Using Iteration
We can use iteration as the simplest approach to solve the list with importing different operators.
Initial list [91, '+', 132, '-', 156, '+', 4] Answer after solving list is: 71.0
Method #2: Using eval and join
Initial list [2, '+', 22, '+', 55, '+', 4] Answer after solving list is: 83
- Python | sympy.solve() method
- Python | Numbers in a list within a given range
- Python program to count Even and Odd numbers in a List
- Python program to print odd numbers in a List
- Python | Sort list of numbers by sum of their digits
- Python program to print even numbers in a list
- Python | Create list of numbers with given range
- Python | Check if list contains consecutive numbers
- Python | Convert Stream of numbers to list
- Python | Multiply all numbers in the list (3 different ways)
- Python | Extract numbers from list of strings
- Python | Largest number possible from list of given numbers
- Python program to print negative numbers in a list
- Python | Make a list of intervals with sequential numbers
- Python program to print positive numbers in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.