0

I was doing a coding challenge and noticed something weird with how arrays worked with modules (or more likely it's my own error).

pages = [*list of 5 webpages*]
final = []

for i in range(0,4):
    page = requests.get(pages[i])
    piece = page.text
    final.append(piece)

print(''.join(final))

This code only joins 4 of the 5 webpages. Changing the range to 0,5 or len(pages) solves the problem. I was under the impression that 0,4 would include all the webpages in my list (5 of them) since indexing starts at 0.

2
  • 3
    Why are you iterating over indices? Just iterate over the pages directly? for page in pages: page = requests.get(page) Commented Feb 9, 2020 at 4:17
  • 3
    Python range doesn't include the "end" value, so range(x, y) means x, x+1, x+2, ..., y-2, y-1. Commented Feb 9, 2020 at 4:18

1 Answer 1

1

The following code is pythonic and more robust in a sense that it does not depend on the number of pages:

pages = [*list of 5 webpages*]
final = [requests.get(page).text for page in pages]
print(''.join(final))
Sign up to request clarification or add additional context in comments.

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.