# Python Lists

> Learn how Python lists work: access items by index, slice them, check length with len(), add items with append() and extend(), and sort them with sort().

Author: Flavio Copes | Published: 2020-12-08 | Canonical: https://flaviocopes.com/python-lists/

Lists are an essential [Python](https://flaviocopes.com/python-introduction/) data structure.

The allow you to group together multiple values and reference them all with a common name.

For example:

```python
dogs = ["Roger", "Syd"]
```

A list can hold values of different types:

```python
items = ["Roger", 1, "Syd", True]
```

You can check if an item is contained into a list with the `in` operator:

```python
print("Roger" in items) # True
```

A list can also be defined as empty:

```python
items = []
```

You can reference the items in a list by their index, starting from zero:

```python
items[0] # "Roger"
items[1] # 1
items[3] # True
```

Using the same notation you can change the value stored at a specific index:

```python
items[0] = "Roger"
```

You can also use the `index()` method:

```python
items.index("Roger") # 0
items.index("Syd") # 2
```

As with strings, using a negative index will start searching from the end:

```python
items[-1] # True
```

You can also extract a part of a list, using slices:

```python
items[0:2] # ["Roger", 1]
items[2:] # ["Syd", True]
```

Get the number of items contained in a list using the `len()` global function, the same we used to get the length of a string:

```python
len(items) #4
```

You can add items to the list by using a list `append()` method:

```python
items.append("Test")
```

or the extend() method:

```python
items.extend(["Test"])
```

You can also use the `+=` operator:

```python
items += ["Test"]

# items is ['Roger', 1, 'Syd', True, 'Test']
```

> Tip: with `extend()` or `+=` don't forget the square brackets. Don't do `items += "Test"` or `items.extend("Test")` or Python will add 4 individual characters to the list, resulting in `['Roger', 1, 'Syd', True, 'T', 'e', 's', 't']`

Remove an item using the `remove()` method:

```python
items.remove("Test")
```

You can add multiple elements using

```python
items += ["Test1", "Test2"]

#or

items.extend(["Test1", "Test2"])
```

These append the item to the end of the list.

To add an item in the middle of a list, at a specific index, use the `insert()` method:

```python
items.insert(1, "Test") # add "Test" at index 1
```

To add multiple items at a specific index, you need to use slices:

```python
items[1:1] = ["Test1", "Test2"]
```

Sort a list using the `sort()` method:

```python
items.sort()
```

> Tip: sort() will only work if the list holds values that can be compared. Strings and integers for example can't be compared, and you'll get an error like `TypeError: '<' not supported between instances of 'int' and 'str'` if you try.

The `sort()` methods orders uppercase letters first, then lowercased letters. To fix this, use:

```python
items.sort(key=str.lower)
```

instead.

Sorting modifies the original list content. To avoid that, you can copy the list content using

```python
itemscopy = items[:]
```

or use the `sorted()` global function:

```python
print(sorted(items, key=str.lower))
```

that will return a new list, sorted, instead of modifying the original list.
