Charming the Python: Lists
Tags: python, beginners
Published: 2019-11-27 17:51:21


*If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.*

---
## Lists

A list is a collection which is ordered and changeable and there may be duplicate items.

### Creating a List

```python
# creating a list with a builtin function
list_name = list('list item 1', 'list item 2', 'list item 3')

# creating a list with brackets
another_list_name = ['list item 1', 'list item 2', 'list item 3']

# you can create an empty list like this
list = []
```
### Accessing list items using indexing
| cat | dog | mouse | cheese | lemon | road
|--|--|--|--|--|--|
| 0 | 1 | 2 | 3 | 4 | 5 |
| -6 | -5 | -4 | -3 | -2 | -1 |

```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']

print(things[0])
>>>cat

print(things[-2])
>>>lemon
```

### Changing Lists
Lists are mutable, or modifiable.

#### Add via Index
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things[0] = 'chair' # adds 'chair' to the beginning of list, at index 0
print(things)  # returns newly modified list
>>>['chair', 'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']
```
#### Add to end
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.append = 'chair' # adds 'chair' to the end of list
print(things)  # returns newly modified list
>>>['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road', 'chair']
```
#### Check
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
does_exist = 'banana' in things  # sees if 'banana' is in the list
print(does_exist)
>>>True
does_exist = 'lime' in things  # sees if 'lime' is in the list
print(does_exist)
>>>False
```
#### Insert
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.insert('pie', 1)  # adds 'pie' at index 1
print(things)
>>>'cat', 'pie', 'dog', 'mouse', 'cheese', 'lemon', 'road'
```
#### Remove
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.remove('mouse')  # removes 'mouse', regardless of index
print(things)
>>>'cat', 'dog', 'cheese', 'lemon', 'road'
```
#### Pop
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.pop(-2)  # removes item referenced by index
print(things)
>>>'cat', 'dog', 'mouse', 'cheese', 'road'
```
#### Delete
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
del things[0]  # deletes item referenced by index
print(things)
>>>'dog', 'mouse', 'cheese', 'lemon', 'road'
```
#### Clear
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.clear()
print(things)  # nothing will show because you've cleared all items from the list
>>>
```

### Joining
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']
more_things = ["table", "book", "blanket"]
new_list = things + more_things  # combines both lists in the order written
print(new_list)
>>>'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road', "table", "book", "blanket"

newer_list = more_things + things
print(newer_list)
>>>"table", "book", "blanket", 'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road'
```

### Counting
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']
print(things.count())
>>>6
```
### Reversing
```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']

things.reverse()  # flip the order of the list
print(things)
>>>'road', 'lemon', 'cheese', 'mouse', 'dog', 'cat'
```
### Sorting

```python
things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']

things.sort()  # alphanumeric order
print(things)
>>>'cat', 'cheese', 'dog', 'lemon', 'mouse', 'road'

things.sort(reverse=True)  # backwards
print(things)
>>>'road', 'mouse', 'lemon', 'dog', 'cheese', 'cat'
```
---
Series based on {% post https://dev.to/asabeneh/30daysofpyton-challenge-43ii %}