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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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))

1 Comment

but it was giving again error like dcode is not defined
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.