Here's an example of where I started
mylist = [["1", "apple"], ["2", "banana"], ["3", "carrot"]]
def testfun(passedvariable):
for row in passedvariable:
row.append("Something else")
return "Other answer"
otheranswer = testfun(mylist)
print mylist
I'd expected mylist
not to have changed.
I then tried this to remove that temporary column, but that didn't work:
mylist = [["1", "apple"], ["2", "banana"], ["3", "carrot"]]
def testfun(passedvariable):
for row in passedvariable:
row.append("Something else")
# I'm now finished with the "Something else" column, so remove it
for row in passedvariable: row = row[:-1]
return "Other answer"
otheranswer = testfun(mylist)
print mylist
I think tried to use a different reference:
mylist = [["1", "apple"], ["2", "banana"], ["3", "carrot"]]
def testfun(passedvariable):
copyofdata = passedvariable
for row in copyofdata:
row.append("Something else")
# I'm now finished with the "Something else" column, so remove it
for row in copyofdata: row = row[:-1]
return "Other answer"
otheranswer = testfun(mylist)
print mylist
I've written little Python scripts for a few months now, but never come across this before. What do I need to learn about, and how do I pass a list to a function and temporarily manipulate it (but leave the original untouched?).