Brendan Berkowitz
Brendan Berkowitz

Reputation: 29

turning a loop into a list python

Below is my code: I am trying to turn the loop results I get from this code into a list. Any ideas?

import requests
from bs4 import BeautifulSoup

page = requests.get('http://forecast.weather.gov/MapClick.php?lat=37.7772&lon=-122.4168')

soup = BeautifulSoup(page.text, 'html.parser')

for x in soup.find_all(class_='tombstone-container'):
    y = (x.get_text())
    print (y)

Upvotes: 1

Views: 11350

Answers (3)

Philipp Lange
Philipp Lange

Reputation: 871

if you don't want to change much of your code, create an empty list before your loop like this

myList = []

and in your loop append the content like this:

myList.append(str(x.get_text())) # EDIT2

EDIT1: The reason I used myList.append(...) above instead of myList[len(myList)] or something similar, is because you have to use the append method to extend already existing lists with new content.

EDIT2: Concerning your problem with None pointers in your list: If your list looks like [None, None, ...] when printed after the for loop, you can be sure now that you have still a list of strings, and they contain the word None (like this ['None','None',...]). This would mean, that your x.get_text() method returned no string, but a None-pointer from the beginning. In other words your error would lie buried somewhere else.

Just in case. A complete example would be:

myList = []
for x in soup.find_all(class_='tombstone-container'):
    # do stuff, but make sure the content of x isn't modified
    myList.append(str(x.get_text()))
    # do stuff

Upvotes: 3

hspandher
hspandher

Reputation: 16743

Just loop over it.

map(lambda x: x.get_text(), soup.find_all(class_='tombstone-container'))

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476709

A straightforward way to convert the results of a for loop into a list is list comprehension.

We can convert:

for x in soup.find_all(class_='tombstone-container'):
    y = (x.get_text())
    print (y)

into:

result = [x.get_text() for x in soup.find_all(class_='tombstone-container')]

Basic (list comprehension has a more advanced syntax) has as grammar:

[<expr> for <var> in <iterable>]

it constructs a list where Python will iterate over the <iterable> and assigns values to <var> it adds for every <var> in <iterable> the outcome of <expr> to the list.

Upvotes: 4

Related Questions