What I'd like to do is the following:
test_dict = dict(value = 1, property = properties_list[value-1] )
Naturally, I couldn't do it this way, since it throws me an error saying that name 'value' is not defined.
You could do it by using an assignment expression (aka “the walrus operator”) which were introduced in Python 3.8.
properties_list = [42, 13] test_dict = dict(value=(_:=1), property=properties_list[_-1]) print(test_dict) # -> {'value': 1, 'property': 42}
2.1m questions
2.1m answers
63 comments
56.6k users