Search code examples
pythonpep8

Is this the cleanest way to write a long list in Python?


def kitchen():
    kitchen_items = [
    "Rice", "Chickpeas", "Pulses", "bread", "meat",
    "Milk", "Bacon", "Eggs", "Rice Cooker", "Sauce",
    "Chicken Pie", "Apple Pie", "Pudding"
    ]

I tried reading PEP8, but the only thing I got from there was -

The closing brace/bracket/parenthesis on multi-line constructs may either line up under the first non-whitespace character of the last line of list

I couldn't really understand what that means. My apologies for not reading it properly.


Solution

  • You need to indent the list contents like this

    kitchen_items = [
        "Rice", "Chickpeas", "Pulses", "bread", "meat",
        "Milk", "Bacon", "Eggs", "Rice Cooker", "Sauce",
        "Chicken Pie", "Apple Pie", "Pudding"
    ]
    

    Or

    kitchen_items = [
        "Rice", "Chickpeas", "Pulses", "bread", "meat",
        "Milk", "Bacon", "Eggs", "Rice Cooker", "Sauce",
        "Chicken Pie", "Apple Pie", "Pudding"
        ]