To my mind, the following code should simply reconstruct the input list, but instead I get a list of three copies of that list.
outlist = []
inputList = ["input1", "input2", "input3"]
def bar(input, outlist):
temp = input
outlist.append(temp)
return outlist
r1 = [bar(i, outlist) for i in inputList]
but my result
r1
Out[28]:
[['input1', 'input2', 'input3'],
['input1', 'input2', 'input3'],
['input1', 'input2', 'input3']]
Here is my I thought this would do:
- Create an empty list
- For each item in the input list, append that to outlist.
- Return the result once it has gone through all three itemps in inputList.
So what am I missing/not understanding here? Why do I get a list of three identical lists, instead of just one of those lists by itself? Sorry if this has been asked before, but if it has, I was unable to find it (probably because I wasn't searching for the right terminology)
EDIT: Sorry if I wasn't clear. My goal is not to create a copy of a list, I just thought it would be a simple example to demonstrate the list of lists result. Please do not mark this as a duplicate of that question, because the list copying is not the subject of the question.
Edit2: My desired output: ['input1', 'input2', 'input3']
In other words, I want to make a list comprehension that iterates over a list, does something with the items in that list, and appends that item to a list which is the output. Here is another example, just to be clear that duplicating the original list is not my point:
outlist = []
inputList = [1, 2, 3]
def bar(input, outlist):
temp = input + 1
outlist.append(temp)
return outlist
r1 = [bar(i, outlist) for i in inputList]
r1
Out[31]:
[[2, 3, 4], [2, 3, 4], [2, 3, 4]]
desired output:
[2,3,4]
Sorry if I'm being thick here..