user3994037
user3994037

Reputation:

How to append list of all results from a For loop?

I'm struggling to get a function of mine to work. Here's the basis of what it is:

#for example:
data=[(b),(c),(d),(e)]

results=[]
for x in data: #this goes through each row of the data
    # body_code executes. This part is mostly just changing types, etc
    a=final_body_code
    results.append(a)
print results

#output should be: results=[(b),(c),(d),(e)] #After changing types of b,c,d,e etc.

#The body of the code does not matter at this point, it's just the appending which i'm
#struggling with. 

However, when I do this it does not seem to append a to the results list. I'm a newbie when it comes to python, so please help!

Upvotes: 0

Views: 10173

Answers (3)

user5609897
user5609897

Reputation: 1

data = [b, c, d, e]

results = []

results.extend(final_body_code(i) for i in data)

return results

Upvotes: -1

NoamG
NoamG

Reputation: 1161

You should add examples.

Maybe you have some problem in a=final_body_code which a result is None

However, a little improvement to @Mrinal Wahal answer, using list comprehension:

results = [final_body_code(i) for i in data]

Upvotes: 3

Mrinal Wahal
Mrinal Wahal

Reputation: 51

I Think This Is What You're Willing To Do.

data=[(b),(c),(d),(e)]

results=[]

for x in data:
    results.append(x)

print results

Upvotes: 0

Related Questions