Reputation: 45
I have a small problem in adding the results printed by a function to a list. The situation is like this. The function prints some results like this
result1
result2
result3
How do I add them into a list, to see them like mylist=['result1', 'result2', 'result3']
? Because, in my case, I am working with a for loop which calculates every case using the function written by me (and prints the results like above) but using append function, it prints something like this:
['result1'], ['result1', 'result2'], ['result1', 'result2', 'result3'].
Of course, using the last result, ['result1', 'result2', 'result3']
, should be perfect for me, but I am getting three lists like above. What ca I do to have only the last list, the one with those three results?
Ok guys, I'm gonna put some code here to be more specific. I have a large string (and I'm not gonna write it here because is not so important). My function is splitting from that string some specific url's. The function is this:
def fileref(string, file):
start=string.find(file) + 28
end=string.find('</ref', start)
file=string[start:end]
return file
I have this list with arguments for the function:
files55_2=['f55_1.jpg', 'f55_2.jpg', 'f55_3.txt', 'f55_4.mp4']
Then, with a for loop I do this:
for i in files55_2:
print fileref(string, i)
which prints me these urls:
https://myapi.net/file1
https://myapi.net/file2
https://myapi.net/file3
https://myapi.net/file4
Now I need a solution to have a list with all these elements, like this:
mylist=['https://myapi.net/file1', 'https://myapi.net/file2', 'https://myapi.net/file3', 'https://myapi.net/file4']
I hope I am more specific now. Thanks anyway for your answers!
Upvotes: 0
Views: 198
Reputation: 22827
In your function you are incrementally building your list and printing out the list after every step, something like this:
l = []
for i in range(1,4):
l.append("result {0}".format(i))
print l
output:
['result 1']
['result 1', 'result 2']
['result 1', 'result 2', 'result 3']
What you want to do is to print out the list after you are done creating it, something like:
l = []
for i in range(1,4):
l.append("result {0}".format(i))
print l
output:
['result 1', 'result 2', 'result 3']
edit: In the provided code, change these lines:
for i in files55_2:
print fileref(string, i)
to:
mylist = []
for i in files55_2:
mylist.append(fileref(string, i))
print mylist
Upvotes: 5
Reputation: 822
You might be better off having your function append the results to the list, then printing outside the function.
>>> def append():
... l = []
... for i in range(3):
... l.append(str(i))
... return l
...
>>> l = append()
>>> l
['0', '1', '2']
Or if you already have the list before running the function, you won't need to return it.
>>> def append(l):
... for i in range(3):
... l.append(str(i))
...
>>> l = []
>>> append(l)
>>> l
['0', '1', '2']
Upvotes: 2
Reputation: 430
In for loop you can use "else" close, which executes after loop has finished:
for ...
...
else
//here you can add your results to list after loop has finished
Upvotes: 1