Home > OS >  How to combine multiple output lists together?
How to combine multiple output lists together?

Time:10-22

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:

    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'

CodePudding user response:

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]]
  • Related