Python Lists

laptop on desk with motivation.jpeg Python classifies lists as a container type. Lists are presented as comma-separated values that are enclosed in square brackets. Here is a list of random letters to illustrate this:

my_list = ["r", "a", "n", "d", "o", "m"]

One thing to keep in my is that lists are zero-indexed. They start with the number zero and count from there. A good rule of thumb when starting out is when you are having trouble iterating through items in a list and you get an error check to see if you are indexing one number too many (meaning if you have 60 items in a list but the 60th one is not there, it may be because it's the 59th item).

To find how long a list is (or how many items are in it) you can use the length function: len(). This can come in handy.

my_list = ["r", "a", "n", "d", "o", "m"]
word_length = len(my_list)
print(word_length)       # prints 6

Lists items can have any data type in them. If you are migrating from a different object oriented programming platform this may surprise and delight you. Strings, Integers and Booleans (oh my!)

Lists can be indexed by referencing an item's index number. Indexing from the beginning of the list to the end is straight forward, keeping in mind that the first item will be at position zero. You can also index from the end of a list by using negative numbers! The last position in the list will be position -1 and the second to the last item will be -2 and so on. That makes python lists very versatile in my opinion.

That's all for now, folks! I'll add to this post when I learn more about lists and have more to share. thanks for reading this far! Happy Coding!

pexels-photo-5717462.jpeg