All, I am trying to create a jagged list in Python 3.x. Specifically, I am pulling a number of elements from a list of webpages using Selenium. Each row of my jagged list ("matrix") represents the contents of one of these said webpages. Each of these rows should have as many columns as there are elements pulled from its respective webpage - this number will vary from page to page.
e.g.
webpage1 has 3 elements: a,b,c
webpage2 has 6 elements: d,e,f,g,h,i
webpage3 has 4 elements: j,k,l,m
...
would look like:
[[a,b,c],
[d,e,f,g,h,i],
[j,k,l,m],...]
Here's my code, thus far:
from selenium import webdriver
chromePath = "/Users/me/Documents/2018/chromedriver"
browser = webdriver.Chrome(chromePath)
url = 'https://us.testcompany.com/eng-us/women/handbags/_/N-r4xtxc/to-1'
browser.get(url)
hrefLinkArray = []
hrefElements = browser.find_elements_by_class_name("product-item")
for eachOne in hrefElements:
hrefLinkArray.append(eachOne.get_attribute('href'))
pics = [[]]
for y in range(0, len(hrefLinkArray)): # or type in "range(0, 1)" to debug
browser.get(hrefLinkArray[y])
productViews = browser.find_elements_by_xpath("// *[ @ id = 'lightSlider'] / li")
b = -1
for a in productViews:
b = b + 1
# print(y) for debugging
# print(b) for debugging
pics[y][b] = a.get_attribute('src') # <------------ ERROR!
# pics[y][b].append(a.get_attribute('src') GIVES SAME ERROR AS ABOVE
del productViews[:]
browser.quit()
Whenever I run this, I get an error on the first iteration of the a in productViews
loop:
line 64, in <module>
pics[y][b] = a.get_attribute('src')
IndexError: list assignment index out of range
From what I can tell, the the integer references are correct (see my debugging lines in the for a in productViews
loop), so pics[0][0]
is a proper way to reference the jagged list. This being said, I have a feeling pics[0][0]
does not yet exist? Or maybe only pics[0]
does? I've seen similar posts about this error, but the only solution I've understood seems to be using .append()
, and even as such, using this on a 1D list. As you can see in my code, I've used .append()
for the hrefLinkArray
successfully, whereas it appears unsuccessful on line 64/65. I'm stumped as to why this might be.
Please let me know:
Why my lines
.append()
and[][]=...
are throwing this error.If there is a more efficient way to accomplish my goal, I'd like to learn!
UPDATE: using @User4343502's answer, in conjunction with @StephenRauch's input, the error resolved and I now and getting the intended-sized jagged list! My amended code is:
listOfLists = []
for y in range(0, len(hrefLinkArray)):
browser.get(hrefLinkArray[y])
productViews = browser.find_elements_by_xpath("// *[ @ id = 'lightSlider'] / li")
otherList = []
for other in productViews:
otherList.append(other.get_attribute('src'))
# print(otherList)
listOfLists.append(otherList)
del otherList[:]
del productViews[:]
print(listOfLists)
Note, this code prints a jagged list of totally empty indices e.g. [[][],[][][][],[],[][][],[][],[][][][][]...], but that is a separate issue - I believe related to my productViews
object and how it retrieves by xpath
... What's important, though, is that my original question was answered. Thanks!