0
expression = ['+', '10', 'x']
variables = {"x": (3,4)}
new_expression = []

for key, value in variables.items():
    key_index = expression.index(key)
    
    for i in range(3,5):
        expression[key_index] = i
        new_expression.append(expression)
        
print("new_expression",  new_expression)  

Result is new_expression = [['+', '10', 4], ['+', '10', 4]]

But I want new_expression = [['+', '10', 3], ['+', '10', 4]]

1
  • Because you keep appending the same list. Commented Jan 13, 2021 at 0:11

1 Answer 1

1

Change new_expression.append(expression) to new_expression.append(expression[:]) can work. The reason is that if you append expression, each element within new_expression will points to the same object. We need to raise a new object by copying.

expression = ['+', '10', 'x']
variables = {"x": (3,4)}
new_expression = []

for key, value in variables.items():
    key_index = expression.index(key)
    
    for i in range(3,5):
        expression[key_index] = i
        new_expression.append(expression[:])
        
print("new_expression",  new_expression)  

You may want to change for i in range(3,5): to for i in value:, as you already define the values in the variables.

Refined version:

expression_template = ['+', '10', 'x']
variables = {"x": (3,4)}
new_expression = []

for key, value in variables.items():
    key_index = expression_template.index(key)
    
    for i in value:
        expression = expression_template[:]
        expression[key_index] = i
        new_expression.append(expression)
        
print("new_expression",  new_expression)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.