# List

_Mutable, ordered series, traditionally of the same type of object._

_Advantages: Mutable and ordered. Easy to understand. Relatively efficient memory usage._

_Disadvantages: Searching is O(n)._

**Creating a list use square brackets**

```python
mylist = [ ]
mylist = [1,2,3]
mylist = [1,2,3,"a"]
mylist = ['a', 'b', 'c', [1,2,3] ]    # 4 elements
```

**Checking membership**

```python
3 in mylist    # True or False
               #  O(n) time complexity
mylist.index(3)  # get the index of a element
```

**Converting from another type**

```python
list('abc')      # ['a','b','c']
list((1,2,3))    # [1,2,3]
list({1,2,3})    # [1,2,3]
```

**Slicing**

```python
A = [0, 1, 2, 3, 4, 5]
A[2:4]      # [2, 3]
A[2: ]      # [2, 3, 4, 5]
A[ :4]      # [0, 1, 2, 3]
A[ :-1]     # [0, 1, 2, 3, 4]
A[-3: ]     # [3, 4, 5]
A[-3:-1]    # [3, 4]
A[1:5:2]    # [1, 3]
A[5:1:-2]   # [5, 3]
# reverse a list
A[ : :-1]   # [5, 4, 3, 2, 1, 0]
# rotate a list
A[3: ] + A[ :3]    # [3, 4, 5, 0, 1, 2]
# shallow copy a list
B = A[:]
```

**Replacing an existing element**

```python
mylist = ['a', 'b', 'c']
mylist[1] = 'z'
mylist           # ['a', 'z', 'c']
```

**Replacing multiple existing elements**

```python
mylist = ['a', 'b', 'c', 'd', 'e', 'f']
mylist[1:3] = 'xyz'     # replace indexes 1 and 2 with x, y, z
mylist # ['a', 'x', 'y', 'z', 'd', 'e', 'f']
```

**Adding an element to the end**

```python
# 1st way
mylist = ['a', 'b', 'c']
mylist.append('d')
mylist                  # ['a', 'b', 'c', 'd']
mylist.append([1,2,3])
mylist                  # ['a', 'b', 'c', 'd', [1,2,3]]

# 2nd way
mylist = ['a', 'b', 'c']
mylist += ['d']
mylist                  # ['a', 'b', 'c', 'd']
```

**Adding multiple elements to the end**

```python
# 1st way
mylist = ['a', 'b', 'c']
mylist.extend([1,2,3])
mylist                  # ['a', 'b', 'c', 'd', 1, 2, 3]

# 2nd way
mylist = ['a', 'b', 'c']
mylist += [1,2,3]
mylist                  # ['a', 'b', 'c', 'd', 1, 2, 3]
```

**Insert a element into a list at specific index**

```python
# 1st way: use insert() function
mylist = ['a', 'b', 'c']
mylist.insert(1, 'e')
mylist               # ['a', 'e', 'b', 'c']

# 2nd way: use slicing
mylist = ['a', 'b', 'c']
mylist[1:1] = 'e'
mylist               # ['a', 'e', 'b', 'c']
```

**Removing an element from the end**

```python
mylist = ['a', 'b', 'c']
mylist.pop()          # returns 'c'
mylist                # ['a', 'b']
```

**Removing an element from any index**

```python
# 1st way
mylist = ['a', 'b', 'c']
mylist.pop(0)        # returns 'a'
mylist               # ['b', 'c']

# 2nd way
mylist = ['a', 'b', 'c']
del mylist[0]        # ['b', 'c']
```

**Removing an element based on its value (rather than its position)**

```python
mylist = ['a', 'b', 'c', 'a', 'a', 'b']
mylist.remove('a')     # Remove the first 'a'
mylist                 # ['b', 'c', 'a', 'a', 'b']
```

**Removing a slice**

```python
mylist = ['a', 'b', 'c']
del mylist[0:1]      # ['c']
```

**Sorting**

```python
# 1st way: in-place
mylist = ['d', 'a', 'c', 'b']
mylist.sort()          # Returns None
mylist                 # ['a', 'b', 'c', 'd']

# 2nd way
mylist = ['d', 'a', 'c', 'b']
mylist = sorted(mylist)
mylist                 # ['a', 'b', 'c', 'd']
```

**Reversing**

```python
# 1st way: in-place
mylist = ['a', 'b', 'c']
mylist.reverse()      # returns None
mylist                # ['c', 'b', 'a']

# 2nd way
mylist = ['a', 'b', 'c']
mylist = list(reversed(mylist))
mylist                # ['c', 'b', 'a']

# 3rd way
mylist = ['a', 'b', 'c']
mylist = mylist[::-1]
mylist                # ['c', 'b', 'a']
```

**Joining strings with delimiter**

```python
mylist = ['a', 'b', 'c']
'*'.join(mylist)     # 'a*b*c'
'...'.join(mylist)   # 'a...b...c'
```

**Iterating over the elements**

```python
# 1st way
mylist = ['a', 'b', 'c']
for item in mylist:
    print(item)

# 2nd way
mylist = ['a', 'b', 'c']
for i in range(len(mylist)):
    print(mylist[i])
```

**Iterating over the sorted elements**

```python
mylist = ['d', 'a', 'c', 'b']
for item in sorted(mylist):
    print(item)
```

**Computing the length of a list**

```python
mylist = ['d', 'a', 'c', 'b']
len(mylist)     # 4
```

**Find max/min value in a list**

```python
mylist = [1, 2.33]
max(mylist)   #2.33
min(mylist)   #1
```
