Get startedGet started for free

Lists

1. Lists in Python

In this chapter, you're going to learn about python lists!

2. Lists - square brackets [ ]

A list in Python is identifiable when you see square brackets with items or elements separated with commas.

3. Python is zero-indexed

Each item in a python list takes an ordered position called an index. Python is zero-indexed, meaning that the first element in any list has an index of zero, and the second element has an index of 1, the third element is 2 and so on.

4. Subset lists

You can subset elements of any list through a process called "indexing". To access an element of a list, you can use the integer representing the index position inside square brackets. To extract the first element of the list months, we type months followed by 0 in square brackets. Similarly, to get the third element, we would type months followed by 2 in square brackets.

5. Negative indexing of lists

You can use a negative index to start counting from the end of a list. For example, to extract the last element from months, you can use months[-1] and months[-2] gives you the second to last element.

6. Subsetting multiple list elements with slicing

In many cases, you'll want to subset more than a single element of a list. This approach is called slicing. The slicing syntax to subset list elements uses the name of the list and a colon along with the indices you wish to manipulate between square brackets as shown here. The first number is the start index of the elements to include up to but not including the element of the second specified index. For example, to slice the second index up to but not including the fifth index in the list months, we would type months followed 2 colon 5 inside square brackets. This command would subset the second index, March, and up to but not including the fifth index, "June". A good way to remember index slicing is that the indexed element before the colon is included but the indexed element after the colon is excluded. Note that you could also produce the same output using negative list indexing. To do this, you can type months followed by negative 4 colon negative 1 inside square brackets.

7. Extended slicing with lists

You can also indicate that you want to include elements from the beginning or to the very end of a list by not explicitly stating an index. For example, if you type months followed by 3 colon inside square brackets, python would interpret this code as asking for elements starting from index 3 up to the end of the list. We call this extended slicing of a list. Similarly, you could request elements from the beginning of the list up to but not including the third indexed element by typing months followed by colon 3 inside square brackets.

8. Slicing with Steps

At times, you may want to skip some elements when slicing a list. For example, you might want to subset every other month in our list months. To do this, you could optionally include an additional argument to the slicing syntax called a step. The step is an integer value which determines the increment between each index for slicing. Here we can see that a step of 2 outputs every other month. If we changed this step to 3, we would output every 3rd month. Note that the step argument is optional -- if it is not explicitly provided, the default assumption is a step size of 1.

9. Let's practice!

Now that you've learned about lists and its syntax, let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.