Python Forum

Full Version: Getting All Items From A List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

In practicing some webscrapping with BeautifulSoup and Selenium.
Using the find_all to extract a class, I get the following list:

Output:
['w_A', 'w_C', 'w_B', 'mr1', 'mt1', 'ph1']
Here's a snippet of the code that get's me this:

mt2_mb2 = soup.find('div', {'class': 'mt2 mb2'})
    span_2 = mt2_mb2.find('span')['class']
    print(span_2)
What I want to do, is get all of those items together. So in other words, I want to get the output to look like this:
Output:
w_A w_C w_B mr1 mt1 ph1
Could someone kindly enlighten me how to do this?

Thanks a lot.
Do you want them as a single string with a space between each string item of the list?
items = ['w_A', 'w_C', 'w_B', 'mr1', 'mt1', 'ph1']
joined_items = ' '.join(items)
print(joined_items)
Output:
w_A w_C w_B mr1 mt1 ph1
Hi Yoriz,

Yes please, as a single string with a space between each string item.
You can use string's join method as I have shown in the example above.
(Sep-25-2021, 12:40 AM)Yoriz Wrote: [ -> ]You can use string's join method as I have shown in the example above.

Sorry Yoriz- just woke up and must be blind as a bat! I didn't see your code and thought you pasted my code in there to query.

Thanks so much. Never heard of the join method- will definitely take a note of that for future.

Have a great one.