Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine multiple output lists together?

I need the code to output a list that has all the words in one list. I’ve googled and tried multiple methods, but nothing works. The solutions I’ve found output multiple lists that contain empty lists inside for example: ["Lorem ipsum dolor"]["Morbi quis dictum"][""]["Tempus commodo"].

What I need it to output is ["Lorem ipsum dolor", "Morbi quis dictum", "Tempus commodo"] When I use zip function it outputs a list containing only letters. For example ["l"]["o"]["r"]["e"].

This is my code right now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    for i in soup.find(class_ = "body").stripped_strings:
        p = repr(i)
        print(p, end="")

And this is the output:
'Lorem ipsum dolor' 'Morbi quis dictum'

>Solution :

Store the results in a list:

out = []
for i in soup.find(class_ = "body").stripped_strings:
    p = repr(i)
    print(p, end="")
    if i and i[0]:
        out.append(i) # or out.append(p)

If you don’t need p, you can do this in one line using list comprehension:

out = [i for i in soup.find(class_ = "body").stripped_strings if i[0]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading