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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- NumPy - Arithmetic operations with array containing string elements
- Python Arithmetic Operators
- Python | Find the tuples containing the given element from a list of tuples
- Python | Count the sublists containing given element in a list
- Python | Remove element from given list containing specific digits
- Python | Remove List elements containing given String character
- Python | sympy.solve() method
- Python program to solve quadratic equation
- Python - Solve the Linear Equation of Multiple Variable
- Solve Sudoku with Computer Vision and Constraint Satisfaction Algorithm
- Python | Counting sign change in list containing Positive and Negative Integers
- Python | Sort list containing alphanumeric values
- Python | Remove tuple from list of tuples if not containing any character
- Python - Remove words containing list characters
- Python | Get the smallest window in a string containing all characters of given pattern
- Get all rows in a Pandas DataFrame containing given substring
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Extracting Tweets containing a particular Hashtag using Python
- Increment and Decrement Operators in Python
- Python Membership and Identity Operators | in, not in, is, is not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.