'Getting an array iteration error, wondering how to fix it
I am currently building a web scraper for Real Estate data. I'm working in Python and I've come across an error I can't seem to be able to fix.
for i in range(len(s)):
if '$' in s[i]:
price.append(s[i])
elif 'bath' in s[i]:
left = s[i].partition(",")[0]
right = s[i].partition(",")[2]
bed_bath.append(left)
sqft_lot.append(right)
elif 'fort collins' in s[i].lower():
address0 = s[i-1]+' '+s[i]
address.append(address0)
elif s[i].lower() == 'advertisement':
del s[i]
else:
continue
Value of 's' being:
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get(realtor.format(format))
p = browser.find_element(By.XPATH, "//ul[@class='jsx-343105667 property-list list-unstyle']")
content = p.text
s = re.split('\n',content)
This is basically supposed to iterate through the array s, and add them to a separate array [price,bed_bath,sqrft_lot,address] to be used in a DataFrame. I know that it is indexing properly, I've printed each line consecutively using for i in range(len(s)): print s[i], which works, but then when I try to implement logic it's just breaking.
Getting error:
if '$' in s[i]:
**IndexError: list index out of range**
Any input into why this is happening would be much appreciated.
Solution 1:[1]
create a new list from s and populate it with filtered and processed data
output_list = []
def process_data(value):
# your code for processing data
...
for i in range(len(s)):
if s[i] == some_condition(i):
output_list.append(process_value(s[i])
Solution 2:[2]
You're deleting inside the for loop. The example here throws an error as well and is maybe easier to understand:
s = [i for i in range(5)]
for i in range(len(s)):
print(f"{i=} with {s=}")
del s[i]
Output:
IndexError: list assignment index out of range
i=0 with s=[0, 1, 2, 3, 4]
i=1 with s=[1, 2, 3, 4]
i=2 with s=[1, 3, 4]
i=3 with s=[1, 3]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roman |
| Solution 2 | Christian Weiss |
