PYTHON LISTS
Published: 2022-05-06 12:41:04


#<u>Python Data Structures</u>

##_1. Lists_

Lists are _mutable ordered data structures_ that are represented by square brackets.

```
my_list = []
```
They can contain any of the data type i.e **int, strings, char, floats, or doubles.**
##<u>Indexing with Lists</u>
Lists use the zero-based indexing method to retrieve elements from the list, where by the first element is assigned an index zero.
_For example:_

```python
fruits = ['Apple','Pitch','Berries']
fruits[0]
>>>Apples
fruits[1]
>>>Pitch
```
_Negative indexing_ can also be used  in accessing elements within the list. For instance:

```python
car_models = ['Ford','Toyota','Ferrari']
car_models[-1]
>>>Ferrari
```
The negative indexing can be used to retrieve the last element in a list easily.
</br>
<u>**Slicing**</u>
Slicing enables us to retrieve multiple elements from our list.
When slicing it's important to note that the lower index is inclusive and the upper index is excluded.
_example:_

```python
artists = ['Future','Minaj','Drake','Travis']
artists[1:3]
>>>['Minaj','Drake']
```
From the above example, we were able to retrieve elements from index 1 to index 2.
One can also start from the first index by omitting the lower index and only providing the upper index.

```python
artists = ['Future','Minaj','Drake','Travis']
artists[:3]
>>>['Future','Minaj','Drake']
```
In this case we were able to retrieve all the elements starting with the first element and stopping at index 2.
Or you can retrieve all the elements starting from the lower index of your choosing and omitting the upper index.
_example:_

```python
artists = ['Future','Minaj','Drake','Travis']
artists[1:]
>>>['Minaj','Drake','Travis]
```
In addition to slicing in lists we can also use the `in` and `not in` methods to check if an element is found in our list or not.

```python
numbers = [1,2,3,4,5,6]
12 in numbers
>>>False
5 in numbers
>>>True
23 not in numbers
>>>True
```
##<u>Mutability In Lists</u>
Unlike strings lists values can be changed.
Deciding whether an object can change the values after it has been created is known as **mutability**.
you can use indexing to change values of within a list.
_example:_

```python
cars = ['Volvo','Mercedes','BMW','Audi']
cars[2] = 'Ford'
print(cars)
>>>['Volvo','Mercedes','Ford','Audi']
```
##<u>**Useful Functions Used With Lists**</u>

`len()` : returns the number of elements in the list
_example:_
```python
cars = ['Volvo','Mercedes','BMW','Audi']
len(cars)
>>>4
```
`max()` : returns the greatest element of the list.
The greatest element in a list is determined by what <u>_type of objects are in the list_</u>. For a case of numbers the `max()` function will return the <u>highest number</u> in the list, while in the case of Strings the `max()` function will return the <u>element that would appear last in the list if the list was sorted alphabetically</u>.
</br>
`min()` : returns the smallest element in a list, min() function is a polar opposite of the max function.
</br>
`sorted()` : returns a copy of the list in order from smallest to largest, leaving the list unchanged.
```python
cars = ['Volvo','Mercedes','BMW','Audi']
print(sorted(cars,reverse=False))
>>>['Audi','BMW','Mercedes','Volvo']
```
`append()` : adds elements into the list
```python
desert = ['biscuits', 'cakes', 'cookies', 'custards', 'gelatins', 'ice creams']
desert.append(['pudding'])
print(desert)
>>>['biscuits', 'cakes', 'cookies', 'custards', 'gelatins', 'ice creams', ['pudding']]
```