Reputation: 3299
The objective is to create a list comprehension that outputted two values.
The for loops look like below
paper_href_scopus = []
paper_title = []
for litag in all_td.find_all('a', {'class': 'ddmDocTitle'}):
paper_href_scopus.append(litag['href'])
paper_title.append(litag.text)
While creating list comprehension as below work without any error
[(paper_href_scopus.append(litag['href']), paper_title.append(litag.text)) \
for litag in all_td.find_all('a', {'class': 'ddmDocTitle'})]
Modifying the list comprehension as below:
paper_href_scopus, paper_title = [(litag['href'], litag.text) \
for litag in all_td.find_all('a', {'class': 'ddmDocTitle'})]
resulting an error of :
ValueError: not enough values to unpack (expected 2, got 1)
May I know how such an error can be avoided?
Upvotes: 0
Views: 61
Reputation: 6590
You can just use zip
like,
>>> paper_href_scopus, paper_title = zip(*[(litag['href'], litag.text) for litag in all_td.find_all('a', {'class': 'ddmDocTitle'})])
Also, no need to create the list
inside, you can use a genexpr
, but i don't think it will matter here. Neverthless:
>>> paper_href_scopus, paper_title = zip(*((litag['href'], litag.text) for litag in all_td.find_all('a', {'class': 'ddmDocTitle'})))
Note:
As suggested by balandongiv, here's a link to genexpr
usage and another link to official docs
Upvotes: 2