Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pep8

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.

like image 870
hky404 Avatar asked Apr 10 '14 05:04

hky404


People also ask

How do you write a long list in Python?

Create Python Lists In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.

Can a list be too long in Python?

According to the source code, the maximum size of a list is PY_SSIZE_T_MAX/sizeof(PyObject*) . On a regular 32bit system, this is (4294967295 / 2) / 4 or 536870912. Therefore the maximum size of a python list on a 32 bit system is 536,870,912 elements.

How do you define a large list in Python?

The most common way to declare a list in Python is to use square brackets. A list is a data structure in Python that contains an ordered sequence of zero or more elements. Lists in Python are mutable, which means they can be changed. They can contain any type of data, like a string or a dictionary.


2 Answers

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"
    ]
like image 123
thefourtheye Avatar answered Oct 12 '22 22:10

thefourtheye


The section you quoted:

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

Honestly, it means exactly what it says:

my_list = [
    'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h',  <-- "the last line of the list"
    ^
    "the first non-whitespace character"

Thus:

my_list = [
    'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h',
    ]

There's also the second option that PEP-8 refers to,

or it may be lined up under the first character of the line that starts the multi-line construct, as in:

"the first character"
v
my_list = [  <-- "line that starts the multi-line construct"
    'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h',

Thus:

my_list = [
    'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h',
]

Personally, I prefer this second style, because it give a nice way to scan for the end of the list: the ] justs back out into the left-hand side:

my_list = [
|    'items', 'items',
|    'items', 'items',
|  < a nice line for your eye to track
|
|
]  < this stands out more
like image 21
Thanatos Avatar answered Oct 12 '22 20:10

Thanatos