How to subset Python Lists

Accessing Information in Python Lists: Understanding Indexes and Slicing

Python lists are powerful data structures that can store multiple values, and accessing information from these lists is crucial for most programming tasks. In this article, we will delve into how to access elements in a list using indexes and slicing.

Indexing Elements in a List

---------------------------

When working with Python lists, each element has an index associated with it. The first element in the list is assigned an index of 0, the second element an index of 1, and so on. This means that to access the height of Emma, which is stored as a float at index 3, you would use `list[3]`. Similarly, to access the string "dad" from the list, which is stored at index 6, you would use `list[6]`.

One advantage of using indexes is that it allows us to count backwards from the end of the list. For example, if we want to get Emma's dad's height (which is at the end of the list), we can use a negative index, such as `-1`. This approach is particularly useful when working with large lists, as it enables us to access elements without having to specify their exact position.

Negative Indexes: Accessing Elements from the End

------------------------------------------------

Python supports negative indexes that count backwards from the end of the list. These indexes are useful when we need to access elements at the end of the list. For instance, if we want to get Emma's dad's height (which is at index `-1`), we can use `list[-1]`. The same applies to accessing other elements at the end of the list.

Using Indexes: Examples and Practice

--------------------------------------

To illustrate how indexes work, let's look at an example. Suppose we have a list containing the names "Emma", "mom", "dad", "son", and "daughter". We can access the height of Emma using `list[3]`, which would return `1.68`. Similarly, to access the string "dad" from the list, we would use `list[6]`.

Here's an example code snippet that demonstrates how indexes work:

```python

list = ["Emma", 1.68, "mom", "dad", 1.71, "son", "daughter"]

print(list[3]) # Output: 1.68

print(list[6]) # Output: "dad"

```

Slicing Elements in a List

---------------------------

Another powerful feature of Python lists is slicing, which allows us to select multiple elements from the list and create a new list containing those elements.

The syntax for slicing involves specifying a range using a colon (:) between two indexes. For example, if we want to get the first three elements of the list, we would use `list[0:3]`. The slice starts at the index specified before the colon and ends at the index specified after the colon.

By default, Python includes all elements up to and including the last index specified in the slice. For instance, if we want to get the last three elements of the list, we would use `list[-3:]`.

Here's an example code snippet that demonstrates how slicing works:

```python

list = ["Emma", 1.68, "mom", "dad", 1.71, "son", "daughter"]

print(list[0:3]) # Output: ["Emma", 1.68, "mom"]

print(list[-3:]) # Output: [1.71, "son", "daughter"]

```

Slicing: Leaving Out Indexes and Specifying Ranges

--------------------------------------------------

There are some nuances to slicing that we need to be aware of.

If we leave out the index before the colon, Python will start the slice from index 0 by default. This means that if we want to get all elements up to a certain point in the list, we can simply omit the starting index.

Conversely, if we leave out the index after the colon, Python will include all elements up to and including the last index specified in the slice. This means that if we want to get all elements from a certain point onwards, we need to specify an ending index.

Here's an example code snippet that demonstrates how slicing works with omitted indexes:

```python

list = ["Emma", 1.68, "mom", "dad", 1.71, "son", "daughter"]

print(list[:3]) # Output: ["Emma", 1.68, "mom"]

```

Conclusion

----------

In conclusion, accessing information from a Python list is crucial for most programming tasks. By understanding indexes and slicing, we can efficiently access elements in a list and create new lists containing specific elements. This article has covered the basics of indexing and slicing, including using negative indexes, specifying ranges, and omitting indexes. With practice and experience, you'll become proficient in using these features to manipulate data in your Python programs.

"WEBVTTKind: captionsLanguage: enafter you've created your very own Python lists you might wonder how you can access information in this list Python uses the index to do this have a look at the familis again here the first element in the list as an x 0 the second element has the next one and so on suppose that you want to select the height of Emma so the float 1.68 it's the fourth elements swear as an extreme to select it you use 3 inside square brackets similarly to select the string dad from the list which is the 7th element you'll need to put the index 6 inside square brackets can also count backwards using negative indexes this is useful if you want to get some elements at the end of your list to get your dad's height for example you'll need to index minus 1 these are the negative indexes for all list elements this means that this line this line return the exact same results apart from indexing there's also something called slicing which allows you to select multiple elements from a list thus creating a new list you can do this by specifying a range using a colon let's first have another look at the list and then try this piece of code can you guess what it will return a list with the float 1.68 the string mom and the float 1.71 corresponding to the 4th 5th and 6th element in the list maybe let's see what the output is apparently only the elements with index 3 & 4 get returns the element with index 5 is not included in general this is the syntax the index you specified before the column so where the slice starts is included while the index you specify after the column where the slice ends it's not with this in minds can you tell what this call will return you probably guessed correctly that this color gives you a list with 3 elements or responding to the elements with index 1 2 & 3 of the fam lists it can also choose to just leave out the index before or after the colon if you leave out index where the slice should begin you're telling Python to start the slice from index 0 like this example if you leave out index for the slice it ends you include all elements up to and include the last element in the list like here now it's time to head over to the exercises or you will continue to work on the list you've created yourself before you'll be using different sub setting methods to get exactly the piece of information you needafter you've created your very own Python lists you might wonder how you can access information in this list Python uses the index to do this have a look at the familis again here the first element in the list as an x 0 the second element has the next one and so on suppose that you want to select the height of Emma so the float 1.68 it's the fourth elements swear as an extreme to select it you use 3 inside square brackets similarly to select the string dad from the list which is the 7th element you'll need to put the index 6 inside square brackets can also count backwards using negative indexes this is useful if you want to get some elements at the end of your list to get your dad's height for example you'll need to index minus 1 these are the negative indexes for all list elements this means that this line this line return the exact same results apart from indexing there's also something called slicing which allows you to select multiple elements from a list thus creating a new list you can do this by specifying a range using a colon let's first have another look at the list and then try this piece of code can you guess what it will return a list with the float 1.68 the string mom and the float 1.71 corresponding to the 4th 5th and 6th element in the list maybe let's see what the output is apparently only the elements with index 3 & 4 get returns the element with index 5 is not included in general this is the syntax the index you specified before the column so where the slice starts is included while the index you specify after the column where the slice ends it's not with this in minds can you tell what this call will return you probably guessed correctly that this color gives you a list with 3 elements or responding to the elements with index 1 2 & 3 of the fam lists it can also choose to just leave out the index before or after the colon if you leave out index where the slice should begin you're telling Python to start the slice from index 0 like this example if you leave out index for the slice it ends you include all elements up to and include the last element in the list like here now it's time to head over to the exercises or you will continue to work on the list you've created yourself before you'll be using different sub setting methods to get exactly the piece of information you need\n"