Trying to add elements to a list inside "for" loop overwrites the same value each time

Question:

Given the following:

data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            print(field['value'])

Returns:

11111
22222
33333
44444
55555
66666
77777

However, I wanted to store the output as list in a separate variable to use later so I did this:

data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            var_for_later = field['value']
            var_for_later = var_for_later.splitlines()
            var_for_later = var_for_later

But instead, it only returns the first element in the array:

11111

Instead of ['11111','22222','33333','44444','55555','66666','77777']

Why is field['value'] now only storing the first element when assigned to a variable?

Asked By: InlLad

||

Answers:

You are assigning the value to a variable, not appending it to list. In order to store the values in list, you need to update your code to create an empty list before the for loop and append the field['value'] to list by using list.append() method. Hence, your code will become:

data = response.json()

my_list = []  # creating empty list
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            my_list.append(field['value'])  # Appending items to the list

which will return you the my_list as list in your desired format as:

[11111, 22222, 33333, 44444, 55555, 66666, 77777]
Answered By: Moinuddin Quadri

Try this:

yourdata = []
data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            yourdata.append(field['value'])

print(yourdata)

I guess you’re pretty new to Python, so I suggest you to watch some tutorials or search for documentation on the internet. E.g. w3schools

Why didn’t your code work?

Have a look at this line: var_for_later = var_for_later It does absolutely nothing. Also, you overwrite var_for_later in every iteration. So it should technically return ‘77777’ and not ‘11111’ in the end.

Are you sure this is the output that corresponds to this code?

Btw, do you really have multiple entrys with the same id?

Answered By: Rafael

Earlier you were printing one field at a time, but then you started replacing the same variable in every loop which is why it jas only one value. var_for_later only exists within that for loop. In stead create your final list outside the loop and append values in every iteration.

out = []
data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            out.append(field['value'])
Answered By: AnkurSaxena
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.