Editing a list in a different file
I am trying to edit a list in a different file in python django. I have a file called models.py and a file called details.py,
details.py:
DATA = [
{'height': '184', 'width': '49'}
{'height': '161', 'width': '31'}
{'height': '197', 'width': '25'}
{'height': '123', 'width': '56'}
{'height': '152', 'width': '24'}
{'height': '177', 'width': '27'}
]
def edit_list(h,w):
for info in DATA:
if info['height'] == h:
info['width'] = w
return True
models.py:
from abc.details import edit_list
height = '161'
new_width = '52'
update_data = edit_list(height, new_width) #this doesn't work, when I check the file nothing changes in the list :/
What is the best approach to make this possible??
(I don't want to import this list to DB and just update the width there, I want the width to update inside the file itself, removing details.py file and creating a new one using python whenever an edit takes place is not possible because few other functions are taking data from the list as well all the time.
You need to indent your statement info['width'] = w, otherwise you will not get the result you expect. More importantly, you need to 'un-indent' your return statement. At the moment, you return from the function after executing the first row in your for statement. The rest of the rows never get executed.
def edit_list(h,w):
for info in DATA:
if info['height'] == h:
info['width'] = w
return True