Skip to content Skip to sidebar Skip to footer

Getting Error: "not Enough Values To Unpack" When Returning Two Outputs Using List Comprehension With Python

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', {

Solution 1:

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

Post a Comment for "Getting Error: "not Enough Values To Unpack" When Returning Two Outputs Using List Comprehension With Python"