Python Forum

Full Version: Append not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
Use different variable in for loop

id = []
 for i in tag:
DOH!!!
Oh dear obviously staring at it for too long!

cheers anbu23
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