Skip to content Skip to sidebar Skip to footer

Outputting Html Unordered List Python

I'm attempting to write a function with python that will take an list of strings that I have defined and display them as a single string that is and HTML unordered list of said str

Solution 1:

You could do something like that:

defprint_ul(elements):
    print("<ul>")
    for s in elements:
        ul = "<li>" + str(s) + "</li>"print(ul)
    print("</ul>")

toppings = ['mushrooms', 'peppers', 'pepparoni', 'steak', 'walnuts', 'goat cheese', 'eggplant', 'garlic sauce'];
print_ul(toppings)

There were some problems with your original code:

  • you did not call the function, so no wonder it didn't do anything
  • even if you did, the function did not actually print anything, it just returned some values
  • the function didn't really take arguments, so it was not re-usable at all

A better (IMO) solution would be to have a function generating the HTML code, and printing the result:

def ulify(elements):
    string = "<ul>\n"for s in elements:
        string += "<li>" + str(s) + "</li>\n"string += "</ul>"returnstringprint(ulify(['thing', 'other_thing']))

You can also read about list comprehensions. It would make working with lists simpler:

def ulify(elements):
    string = "<ul>\n"string += "\n".join(["<li>" + str(s) + "</li>"for s in elements])
    string += "\n</ul>"returnstring

Solution 2:

Looks like you are trying to build a website. Why don't you use a template engine, like Jinja 2 for this, instead of printing a HTML snippet from a function? For that you will need a Python web application, plausibly written in one of web frameworks. I'd go for Flask here, it's simple to start working with and Jinja is a default template engine for Flask.

If you just want to generate static HTML files, I would recommend Frozen-Flask, which allows you to generate static HTML files that can be hosted without a need to deploy any Python web application on your server. Just copy generated files to your hosting and you are ready to go.

If you still want to just print a HTML snippet, your code should be something like Ealhad posted in his answer.

Also, you original code contains a few problems:

defpizzatoppings(self):
    # you don't need semicolons in Python
    toppings = ['mushrooms', 'peppers', 'pepparoni', 'steak', 'walnuts', 'goat cheese', 'eggplant', 'garlic sauce']
    # you need to initialize a "ul" variable
    ul = "<ul>"for s in toppings:
        ul += "<li>"+str(s)+"</li>"# following two lines where indented too much. In Python, indentation defines a block of code
    ul += "</ul>"return ul

Solution 3:

I was just doing this at work today. Here's my two-line solution:

defexample(elements):
    return'<ul>\n' + '\n'.join(['<li>'.rjust(8) + name + '</li>'for name in elements]) + '\n</ul>'

Which gives:

<ul>
    <li>mushrooms</li>
    <li>peppers</li>
    <li>pepparoni</li>
    <li>steak</li>
    <li>walnuts</li>
    <li>goat cheese</li>
    <li>eggplant</li>
    <li>garlic sauce</li>
</ul

This makes the generated HTML code a little easier to read.

Post a Comment for "Outputting Html Unordered List Python"