Amateur1
Amateur1

Reputation: 67

Appending an empty list to a dictionary

I am looping through a soup and would like to add elements to a dictionary. As of now my code looks like this:

  dict1 = {"Film Director: ":[], "Film Producer: ":[], "Film cast: ":[]}
  info = soup2.find("table", class_="something")
  for tr in info.find_all("tr"):
      columns = tr.find_all("td")[0].text
      values = tr.find_all("td")[1].text
      dict1[columns].append([values])

The problem is that some sites contain more info than others whereby an output can be somethin like this

{'Film Director: ': [['tom'], ['phil'], ['daniel']], 'Film Producer: ': [['mike'], ['kay'], ['banks']], 'Film cast: ': [['luke, shadraa, shwarma'], ['mohammed, abdul, sarkis']]}

How do I also add an empty list to the film cast key? I want this so that I will be able to create an accurate dataframe later.

Upvotes: 0

Views: 440

Answers (1)

balderman
balderman

Reputation: 23825

Use extend

dict1[columns].extend([values])

so you will not have nested lists

Upvotes: 2

Related Questions