Python Tips when Dealing with Lists

Here are some tips when using Python and dealing with lists.

  1. Use snake_case for list variable names. For more information on coding style in Python, see the PEP 8 style guide here.
  2. Never call a variable "list". (If you are using a variable for a list, use something like small_list or big_list -- not "list".) Any word that is not a reserved word and not a built-in data type will work. You need to know the reserved words in Python because if you accidentally use them, it can cause a problem. To see words you should not try to use as variables, click here.
  3. To create a multi-dimensional array based on a variable input "n", use syntax like this:
    cool_list = [[0 for x in range(n)] for y in range(n)]
  4. To print out the contents of each row of the multi-dimensional list, see this posting.

Leave a comment

Your email address will not be published. Required fields are marked *