You cannot use assignments in a expression, and a lambda only takes an expression.

You can store lambdas in dictionaries just fine otherwise:

dict = {'Applied_poison_rating_bonus' : (lambda target, magnitude: target.equipmentPoisonRatingBonus + magnitude)}

The above lambda of course only returns the result, it won't alter target.equimentPoisonRatingBonus in-place.

Answer from Martijn Pieters on Stack Overflow
🌐
Quora
quora.com › How-can-I-use-lambda-function-in-list-and-dictionary-comprehension-in-Python
How to use lambda function in list and dictionary comprehension in Python - Quora
Answer (1 of 3): you can use a lambda function in a Python list comprehension as follows: >>> [(lambda x:x*x)(x) for x in range(1,4)] [1, 4, 9] But there is no point in doing so when the comprehension essentially gives you the lambda for free (you simply write the expression that would have be...
Discussions

Lambda function dictionary update - Ideas - Discussions on Python.org
Updating a dictionary using lambda function doesn’t take d[‘key’]=‘value’ instead it only considers d.update({‘key’:‘value’}) within the lambda function. When you update a dictionary using normal method. you can do it so by d[‘key’]=‘value’ But the same method doen’s ... More on discuss.python.org
🌐 discuss.python.org
0
November 7, 2024
dictionary - lambda in python can iterate dict? - Stack Overflow
Also, note that using map and sorted and other iterating functions (enumerate, etc.) will also use for, somewhere under the hood. ... You don't iterate with lambda. There are following ways to iterate an iterable object in Python: ... Comprehension, including list [x for x in y], dictionary {key: ... More on stackoverflow.com
🌐 stackoverflow.com
January 31, 2025
python: dict of (lambda) functions - Stack Overflow
I've experienced some strange behaviour when storing lambda functions into a dictionary: If you try to pass some default value to a function in a loop, only the last default value is being used. ... #!/usr/bin/env python # coding: utf-8 def myfct(one_value, another_value): "do something with ... More on stackoverflow.com
🌐 stackoverflow.com
December 19, 2022
dictionary - Lambda function in Python using dictionaries - Stack Overflow
I need to convert two functions into lambda versions of the same function, I'll post the code and my attempt that isn't working. This is using dictionaries in Python and mapping pairs and then redu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › lambda function in a dictionary is confusing
r/learnpython on Reddit: Lambda function in a dictionary is confusing
November 27, 2020 -
''' Here's the dictionary for a program I'm building. I'm getting a TypeError 
saying  int object is not callable when I try to run the second print function, \
but the first one works fine. What's the difference between the two and is there 
a way for this to work without a problem?'''

formulas = {1: lambda x: x(x+1)/2,
            2: lambda x: x**2,
            3: lambda x: x(3 * x - 1)/2,
            4: lambda x: x(2 * x - 1),
            5: lambda x: x(5 * x - 3)/2,
            6: lambda x: x(3 * x - 2)
}

print(formulas[2](4))
print(formulas[4](4))
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... A lambda function is a small anonymous function....
🌐
Medium
medium.com › @miguel.amezola › processing-objects-in-a-dictionary-with-lambda-functions-and-higher-order-functions-91aa59be76b5
Processing Objects in a Dictionary with Lambda Functions and Higher-Order Functions | by Miguel Amezola | Medium
February 11, 2023 - The lambda function lambda x, y: x[1] if x[1].age > y[1].age else y is used to find the tuple with the Person object with the maximum age, and reduce successively applies this function to the tuples in the list until all tuples have been processed.
🌐
Python.org
discuss.python.org › ideas
Lambda function dictionary update - Ideas - Discussions on Python.org
November 7, 2024 - Updating a dictionary using lambda function doesn’t take d[‘key’]=‘value’ instead it only considers d.update({‘key’:‘value’}) within the lambda function. When you update a dictionary using normal method. you can do it so by d[‘key’]=‘value’ But the same method doen’s work when using lambda function lambda x,y: d[‘x’]=y for x,y in args / kwargs Instead it takes this as lambda x: d.update({‘x’,y}) for x,y in args/kwargs I’ve found about it recently , i want to make a suggestion if the d[‘x’...
🌐
LabEx
labex.io › tutorials › python-how-to-use-lambda-functions-to-update-dictionary-values-in-python-398266
How to use lambda functions to update dictionary values in Python | LabEx
November 14, 2025 - In this tutorial, we will explore how to use lambda functions to update dictionary values in Python. Lambda functions are compact, anonymous functions that can make your code more concise and readable when working with dictionaries.
Find elsewhere
🌐
Geekflare
geekflare.com › development › how to use lambda functions in python [with examples]
How To Use Lambda Functions in Python [With Examples]
December 29, 2024 - In Python, all iterables: lists, tuples, strings, and more, follow zero-indexing. So the first item is at index 0, the second item is at index 1, and so on. We’d like to sort by the value, which is the price of each item in the dictionary. In each tuple in the price_dict_items list, the item at index 1 is the price. So we set the key to lambda x:x[1] as it’ll use the item at index 1, the price, to sort the dictionary.
🌐
YouTube
youtube.com › watch
Python 3 Tutorial: How To Use Lambda In List and Dictionaries - YouTube
In this Python 3 tutorial, we will use lambda in list and dictionaries. Using the lambda in data structures can very useful. Be sure to like, share and comme...
Published   October 12, 2017
🌐
DataCamp
datacamp.com › tutorial › python-lambda-functions
Python Lambda Functions: A Beginner’s Guide | DataCamp
December 5, 2023 - Then, we use map() with a lambda function to compute total_sales by multiplying the price and quantity for each item in the sales_data dictionary. The **record syntax unpacks the original dictionary, ensuring that all its keys and values are preserved in the new dictionary.
Top answer
1 of 2
16

The fix:

def make_closure(number):
    return lambda x: myfct(x, number)

used as

{'add_{}'.format(number): make_closure(number) for number in range(10)}

The reason for this behaviour is, that the variable number (think: named memory location here) is the same during all iterations of the loop (though its actual value changes in each iteration). "Loop" here refers to the dictionary comprehension, which internally is based on a loop. All lambda instances created in the loop will close over the same "location", which retains the value last assigned to it (in the last iteration of the loop).

The following code is not what actually happens underneath. It is merely provided to shed light on the concepts:

# Think of a closure variable (like number) as being an instance
# of the following class

class Cell:
    def __init__(self, init=None):
        self.value = None

# Pretend, the compiler "desugars" the dictionary comprehension into
# something like this:

hidden_result_dict = {}
hidden_cell_number = Cell()

for number in range(10):
    hidden_cell_number.value = number
    hidden_result_dictionary['add_{}'.format(number)] = create_lambda_closure(hidden_cell_number)

All lambda closures created by the create_lambda_closure operation share the very same Cell instance and will grab the value attribute at run-time (i.e., when the closure is actually called). By that time, value will refer to last value ever assigned to it.

The value of hidden_result_dict is then answered as the result of the dict comprehension. (Again: this is only meant as be read on a "conceptual" level; it has no relation to the actual code executed by the Python VM).

2 of 2
3

number is a variable which has different value for each iteration of the dict comprehension. But when you do lambda x: myfct(x, number), it does not use value of number. It just creates a lambda method that will use value of number when it will be called/used. So when you use you add_{} methods, number has value 9 which is used in every call to myfct(x, number).

🌐
Enterprise DNA
blog.enterprisedna.co › python-dictionary-comprehension-tutorial
Python Dictionary Comprehension Tutorial – Master Data Skills + AI
As you can see, comprehensions provide a concise and more readable way of creating dictionaries in Python. We can also use Lambda functions to create dictionaries. Lambda functions are a way to create small, anonymous functions in Python.
🌐
Learn By Example
learnbyexample.org › python-lambda-function
Python Lambda Function - Learn By Example
April 23, 2020 - Learn to create a Lambda function in Python, pass multiple arguments, return multiple values, combine with map() and filter(), sort iterables, nested lambdas, jump tables and many more.
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python › define lambda function handler in python
Define Lambda function handler in Python - AWS Lambda
January 31, 2026 - To see the event format for a particular service, refer to the appropriate page in the Invoking Lambda with events from other AWS services chapter. If the input event is in the form of a JSON object, the Lambda runtime converts the object to a Python dictionary.
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-4.php
Python: Sort a list of dictionaries using Lambda - w3resource
December 1, 2023 - # Create a list of dictionaries named 'models', each dictionary representing a mobile phone model with 'make', 'model', and 'color' keys models = [ {'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'} ] # Display a message indicating that the following output will show the original list of dictionaries print("Original list of dictionaries:") print(models) # Sort the list of dictionaries ('models') based on the value associated with the 'color' key in each dictionary # Uses the 'sorted()' function with a lambda function as the sorting key to sort based on the 'color' value sorted_models = sorted(models, key=lambda x: x['color']) # Display a message indicating that the following output will show the sorted list of dictionaries print("\nSorting the List of dictionaries:") print(sorted_models)
🌐
GeeksforGeeks
geeksforgeeks.org › python › ways-sort-list-dictionaries-values-python-using-lambda-function
Ways to sort list of dictionaries by values in Python - Using lambda function - GeeksforGeeks
November 14, 2025 - A lambda function is a small anonymous function with any number of arguments and a single expression that is returned. ... Input: [{"name": "Harry", "marks": 85}, {"name": "Robin", "marks": 92}, {"name": "Kevin", "marks": 78}] Output: (sorted ...
🌐
Stack Overflow
stackoverflow.com › questions › 50708170 › defining-lambda-function-from-dictionary-in-python
Defining Lambda function from dictionary in python - Stack Overflow
October 12, 2017 - so that when I call it with the dictionary with numerical values eval can substitute them correctly as following: subs_dict={'sym1':1 , 'sym2':5 , ......) expr = #some text eqn of the syms x = fn(expr,**subs_dict) print(x) However, when I do this, x is printed as function of the symbols... Why??! And what should I do? ... Why does this need to be a lambda instead of a def? And why are you trying to do your own substitution and then do a Python eval instead of asking sympy to substitute and eval for you?