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 print even numbers in a list
- Python | Multiply all numbers in the list (3 different ways)
- Python program to print odd numbers in a List
- Python program to count Even and Odd numbers in a List
- Python | Largest number possible from list of given numbers
- Python | Convert Stream of numbers to list
- Python | Extract numbers from list of strings
- Python | Check if list contains consecutive numbers
- Python | Create list of numbers with given range
- Python | Sort list of numbers by sum of their digits
- Python program to print negative numbers in a list
- Python program to print all Strong numbers in given list
- Python program to print positive numbers in a list