---
jupyter:
  jupytext:
    split_at_heading: true
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
      format_version: '1.0'
      jupytext_version: 1.0.0-rc0
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
---

# More on lists

This section covers some more operations on lists.   See
[lists](../data-types/lists) for an introduction to lists.

## Removing values from a list

Another common operation on lists, is finding and removing values.

### Removing values by index

The `pop` method of a list, removes a value at a given position in the list, and returns that value.

```{python}
my_list = [9, 10, 3, 0]
my_list
```

By default, with no arguments, it removes and returns the last value in the
list:

```{python}
last_value = my_list.pop()
last_value
```

The list has lost the last value.

```{python}
my_list
```

You can give `pop` an argument, which is the position from which you want to
get the value.   `pop` removes the element at that position, shortening the
list by one element.

```{python}
mid_value = my_list.pop(1)
mid_value
```

```{python}
my_list
```

### Removing values by finding

Sometimes we want to remove a certain specific value from the list.

For example, let's say I have this list:

```{python}
my_list = [9, 10, 999, 0]
```

I realize that I don't want the 999 value.  I can `remove` it, using the `remove` method of the list:

```{python}
my_list.remove(999)
```

This modifies my list:

```{python}
my_list
```

What happens if I have more than one value that matches my argument to `remove`?

```{python}
my_list = [9, 10, 999, 0, 999]
my_list.remove(999)
```

We lost the *first* value that matches:

```{python}
my_list
```

## Counting number of occurrences in a list

The `count` method of the list *counts* the number of occurrences of
a particular value.  The argument to the `count` method is the value you want
to check for.

```{python}
my_list = [9, 10, 999, 0, 999]
my_list.count(999)
```

Now we remove one of the 999 values, and count again:

```{python}
my_list.remove(999)
my_list
```

```{python}
my_list.count(999)
```
