0

I am learning python. I want to store one by one element in array or list. this is my code.

for u in durl:
      p2 = requests.get(u)
      tree = html.fromstring(p2.content)
      dcode.append = tree.xpath(ecode)
      print(dcode)

in dcode variable the elements are overriding not appending. i want to insert it one by one. please help me.

virushree
  • 83
  • 1
  • 6

3 Answers3

2

append is a method not a variable so if you want to append tree.xpath(ecode) to dcode then you should write dcode.append(tree.xpath(ecode)) rather than dcode.append = which is an assignment not a method call.

Owen Doyle
  • 175
  • 8
0

tree.xpath(...) returns a list so if you want to merge it with an existing list dcode of selected elements, you might do

dcode.extend(tree.xpath(ecode))
ewcz
  • 12,819
  • 1
  • 25
  • 47
0

You can do it this way, it appends new value to the list and it makes more sense...sort of data structure

def main(decode=[]):
  for u in durl:
      p2 = requests.get(u)
      tree = html.fromstring(p2.content)
      dcode.append(tree.xpath(ecode))
      print(dcode)