How to group elements of loop in a single list index Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to group elements of loop in a single list index without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a for loop in python which extracts data using beautifulsoup from a website and appends them into a list. I am trying to scrap tags from event names ex: AI, Big Data, ML etc.

My code:

import requests
from bs4 import BeautifulSoup

URL = "https://aiml.events/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'lxml')

# Scrape Event Tags
event_tags_list = []
event_tag_div = soup.find_all('div', class_ = 'card-body')
for event_div in event_tag_div:
  event_span = event_div.find_all('span', class_  = 'badge badge-light badge-pill')
  for event_tags in event_span:
    print(event_tags.text)
     

Tags I want to fetch

I am able to fetch the tags but they are all independent. I want to be able to group them together. Currently my list is like this:

tag_list = ['Artificial Intelligence', 'Artificial Intelligence','Machine Learning', 'Healthcare', 'Artificial Intelligence','Public Sector' ] 

My expectation:

tag_list = ['Artificial Intelligence', 'Artificial Intelligence,Machine Learning, Healthcare', 'Artificial Intelligence,Public Sector' ] 

Any help is appreciated. Sorry if the question is too basic.

Answer

Replace the inner loop with a generator that you join into a string.

for event_div in event_tag_div:
    event_span = event_div.find_all('span', class_  = 'badge badge-light badge-pill')
    event_tag_list.append(','.join(event_tag.text for event_tag in event_span))
We are here to answer your question about How to group elements of loop in a single list index - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji