Reputation: 3538
I'm new to writing classes in Python and had a quick question. Here is some code below.
class Parse_profile():
def __init__(self, page):
self.page = page
def get_title(self):
list_of_titles = []
for title in self.page.find_all(.....):
list_of_titles.append(title.get_text())
return list_of_titles
def get_companies(self):
list_of_companies = []
for company in self.page.find_all(.....):
list_of_companies.append(company)
return list_of_companies
I want to create a third function that will take in both list_of_companies
and list_of_titles
(and more later on) and combine them into one list.
How do I do that?
Upvotes: 1
Views: 48
Reputation: 78780
def get_companies_and_titles(self):
return self.get_companies() + self.get_title()
This will get you a list with all companies followed by all titles. In case you want (company, title) tuples in your result-list, use:
def get_companies_and_titles(self):
return zip(self.get_companies(), self.get_title())
The second option can yield unexpected results if the lists are not of the same length. Have a look at the documentation for zip
and izip_longest
in that case.
Upvotes: 3