Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append not working
#1
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)
Reply
#2
Use different variable in for loop

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

cheers anbu23
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,523 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append not working as expected teachinggeek 2 2,220 Apr-09-2020, 04:32 AM
Last Post: buran
  append no working pythonduffer 3 2,701 Apr-12-2019, 06:32 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020