Python Forum
Append not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Append not working (/thread-26553.html)



Append not working - WiPi - May-05-2020

Hi guys,

I have used this several times before and I simply cannot see what I am doing wrong here.
Previous code has extracted a list of data (data_id) under 'tag' - I just want to retrieve it and put it in an array.
Here's my code and output.
id = []
 for id in tag:
                data_id = id.get('data-id')
                print(id.append(data_id),str(data_id))
                #load id array list
                id.append(data_id)
Output:
None 81568 None 176573 None 123466 None 124106 None 125505 None 161249 None 31776
The data is correct, just not being added to array (or rather being added as None)


RE: Append not working - anbu23 - May-05-2020

Use different variable in for loop

id = []
 for i in tag:



RE: Append not working - WiPi - May-05-2020

DOH!!!
Oh dear obviously staring at it for too long!

cheers anbu23


RE: Append not working - deanhystad - May-05-2020

append is a function that returns None. It does not return the list with an appended value.

Your code does not make any sense. It does this:
tag[N].append(tag[N].get('data_id') for N in range(len(tag))

id = []  #<-  This list is thrown away in the next line
for id in tag:
    data_id = id.get('data-id')
    print(id.append(data_id),str(data_id))
    #load id array list
    id.append(data_id)  # This is id from the for loop