When programming with Python, the understanding of the “SyntaxError: cannot assign to function call” error boils down to understanding these three topics:
- What is SyntaxError?
- Understanding variable assignment and
- How to call a function.
Let’s discuss those three before we go into the actual causes of the “Can’t Assign to Function Call” error.
What is SyntaxError?
A SyntaxError is a Python error raised when our code violates the language’s predefined rules.
Understanding variable assignment
One of the Python rules governs how to define a variable. It states that (paraphrased):
“When declaring a variable, the variable name comes first, followed by an assignment operator (=), then the value“.
Going against that rule leads to SyntaxError.
Function call
A Python function is called by using its name followed by parentheses. If the function accepts arguments, then pass the arguments inside the parenthesis, for example.
1 2 3 4 |
def multiply1(a, b): return a * b # Function call - calling the multiply1 function with the two arguments. multiply1(4, 6) |
Note that a Python function returns a value (thus, a function call evaluates to a value). Therefore, we can assign the function call to a variable, as shown below.
1 2 |
val1 = multiply1(5.6, 6) print(val1) |
Output:
33.599999999999994
We are now ready to discuss some common causes of “Can’t Assign to Function Call” and their solutions.
Causes and Solutions to “Can’t Assign to Function Call” Error
This Section discusses four common causes of the error and how to solve each.
Case 1: Specifying a function call to the left-hand side of the assignment operator (=)
As stated earlier, the variable assignment rule requires the variable name to be on the left side of the “=” operator and the value on the right.
That is also true for the function call (because it returns a value) – a function call should be defined on the right-hand side of the assignment operator. Doing the opposite leads to the “Can’t Assign to Function Call” error, for example.
1 2 3 4 5 6 |
def ApplyDiscount(amount): if amount > 1000: return amount - (0.1 * amount) return amount - (0.02 * amount) # Wrong way to assign a function call ApplyDiscount(2150) = result3 |
Output:

1 2 |
# Another wrong way ApplyDiscount(2150) = 456 |
Solution
The correct way to assign a function call is to keep it on the right side of the “=” operator and the variable name on the left. For example,
1 2 3 4 5 6 |
# Correct way of assigning a function call result1 = ApplyDiscount(1350) print(result1) # Another right way result2 = ApplyDiscount(260) print(result2) |
Output:
1215.0 254.8
Case 2: Confusing comparison and assignment operator
Sometimes, you use the assignment operator (=) when you meant to use the comparison operator (==).
1 2 3 4 |
def Add2(a, b): return a + b # This leads to the error Add2(3, 4) = 7 |
If you want to compare the result of the function call with another value, use the comparison operator (==), as shown below.
print(Add2(3, 4) == 7)
Output:
True
Add2(3,4) returns 7, therefore, Add2(3, 4) == 7 evaluates to True.
Case 3: Using square brackets when assigning an item to a dictionary or list
You can also face the “Can’t Assign to Function Call” error when you attempt to assign or add an item into a dictionary using parentheses, as shown below.
1 2 |
dict1 = {"Id": 1, "City": "San Diego", "Name": "Allan"} dict1("City") = "New York" |
Output:
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
You will also experience the same when updating an element in a list.
1 2 |
lst1 = [1, "San Diego", "Allan"] lst1(0) = 5 |
Output:
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Solution
Use square brackets (not parentheses) to update an item in a list or Python dictionary, as shown in the following examples.
1 2 3 4 5 6 7 8 |
lst1 = [1, "San Diego", "Allan"] lst1[0] = 5 # Update the first element in the list. print(lst1) dict1 = {"Id": 1, "City": "San Diego", "Name": "Allan"} # Update an item on a dictionary. dict1["City"] = "New York" print(dict1) |
Output
[5, 'San Diego', 'Allan'] {'Id': 1, 'City': 'New York', 'Name': 'Allan'}
Case 4: Incorrect syntax when using the “in” operator in a loop
Here is an example of that.
1 2 3 4 |
def name1(name): return list(name) result = [i for name1("Smith") in i] print(result) |
Output:
SyntaxError: cannot assign to function call
Note that the order in the list comprehension is incorrect. We should iterate over the result of a function call, not over the element of the return value of the function. The correct code should be
1 2 3 4 |
def name1(name): return list(name) result = [i for i in name1("Smith")] print(result) |
Output:
['S', 'm', 'i', 't', 'h']
Conclusion
This post discussed the causes and solutions to the “SyntaxError: cannot assign to function call” error. After going through the four common cases that lead to the error discussed in this article, you should be able to identify and fix this error when it arises.