The “typeerror: bad operand type for unary -: ‘list’” is an error message in Python.
Are you tired and exhausted from trying to figure out how to solve it? Well, this article is your answer.
This is because in this article we are going to explore the solutions to troubleshoot the “bad operand type for unary ‘list’” error message.
Continue reading as we are going to discuss what this error is all about, why it occurs, and certainly the effective solutions.
Before we dive into the solution, it is good to have a better understanding of this error.
So that next time if you experience it in the future, you already know how to troubleshoot it.
What is “typeerror: bad operand type for unary -: ‘list’” error message?
The “typeerror: bad operand type for unary -: ‘list’” is a common error that occurs when you’re trying to use the unary minus (-) operator on a list, which is not a valid operation.
In addition to that, this error happens when Python encounters an incompatible operand type for the unary minus (-) operator, which is designed to work with numbers but not with lists.
For further understanding…
The unary minus operator in Python is designed to work with numbers, allowing you to perform negation or subtraction operations.
However, it cannot be applied directly to a list, which is a collection of elements.
Why does this error occurs?
The “bad operand type for unary ‘list’” error message occurs for various reasons, such as:
→ When you’re not using the unary minus operator correctly in your code, such as by applying it to a list instead of a number.
→ When the list contains elements of unexpected data types, such as strings or other non-numeric values. Python may not be able to perform the unary minus operation and raise this error.
→ When you’re trying to perform operations that involve mismatched data types, such as subtracting a list from an integer or vice versa.
→ When there are incorrect syntax or typo in your code, such as missing parentheses or improper logic, it can lead to this error.
How to fix typeerror: bad operand type for unary -: ‘list’
In this section, you’ll see the different solutions to the “bad operand type for unary ‘list’” error message that keeps on bothering you.
You may select which solution you will use to fix the error. All of the solutions below are effective in getting rid of the error.
1. Convert the list to a numeric data type
Convert the list to a numeric data type before applying unary minus.
You can easily convert the list elements to a numeric data type, such as integers or floats, before applying the unary minus operator.
For example:
sample_list = ['10', '20', '30', '40', '50']
result = [-int(num) for num in sample_list]
print(result)
Output:
[-10, -20, -30, -40, -50]2. Use a loop to apply unary minus to list elements individually
You can simply use a loop to iterate through each element in the list and apply the unary minus operator to them individually.
sample_list = [10, 20, 30, 40, 50]
result = []
for num in sample_list:
result.append(-num)
print(result)
Output:
[-10, -20, -30, -40, -50]3. Use a list Comprehension to apply unary minus to list elements
You can use a list comprehension to apply the unary minus operator to list elements on a single line. That is an easy way to create a new list.
sample_list = [10, 20, 30, 40, 50]
result = [-num for num in sample_list]
print(result)
Output:
[-10, -20, -30, -40, -50]4. Convert the list to a compatible data type
In order to use the unary minus operator on a list, you need to convert it first to a compatible data type, such as a NumPy array or a Pandas DataFrame.
import numpy as np
sample_list = [10, 20, 30, 40, 50]
my_array = np.array(sample_list)
result = -my_array
print(result)
Output:
[-10 -20 -30 -40 -50]5. Use map() function
The map() function can also be used to apply a function to each element in a list.
sample_list = [10, 20, 30, 40, 50] result = list(map(lambda x: -x, sample_list)) print(result)
Output:
[-10, -20, -30, -40, -50]Frequently Asked Questions (FAQs)
The “typeerror: bad operand type for unary -: ‘list’” error typically occurs when you’re trying to use the unary minus operator on a list in Python.
When you’re using the unary minus operator correctly but still encounter the “typeerror: bad operand type for unary -: ‘list’” error.
Maybe there’s a problem with the data type of the list itself.
Conclusion
By executing the different solutions that this article has already given, you can definitely fix the “typeerror: bad operand type for unary -: ‘list’” error in Python.
We are hoping that this article provides you with sufficient solutions.
You could also check out other “typeerror” articles that may help you in the future if you encounter them.
- Typeerror: super expression must either be null or a function
- Typeerror: ‘index’ object is not callable
- Typeerror: ‘bytes’ object cannot be interpreted as an integer
Thank you very much for reading to the end of this article.
Frequently Asked Questions
Official documentation
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
