0

I want to append dictionary values to a list from a for loop, though it is only picking up the last value. Please advise, here is my code:

for line in obj:
    test = float(line['value'])
    print(test)

a = []
a.append(test)

2 Answers 2

3

Huh! You have messed up the indentation. Indentation is VERY IMPORTANT in python. a list is outside the for-loop hence it would only have the last value of test. IT should be inside the for-loop.

It should be like this -

a = []
for line in obj:
    test = float(line['value'])
    print(test)
    a.append(test)
print a
Sign up to request clarification or add additional context in comments.

Comments

1

Your call to append is outside the loop. Indentation matters in Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.