Python – Data Structures: Lists

Lists

Similar to an array from other languages.

We create a list using the square brackets.

`days_of_week = ["Monday", "Tuesday", "Wednesday", Thursday", "Friday", "Saturday", "Sunday"]`

To access the elements in the list we use bracket notation and zero indexing. Meaning that we start counting at 0.

```days_of_week[0] = Monday
days_of_week[3] = Thursday```

We can also index from the end of the list by using negative indexes.

`days_of_week[-1] = Sunday`

Note: Lists don’t have to contain the same data type they can be mixed

`stuff = ["airplane", 23, True, 3.6]`

Lists and Slice

Lists can be sliced by using their indices

`days_of_week = ["Monday", "Tuesday", "Wednesday", Thursday", "Friday", "Saturday", "Sunday"]`

We can slice the weekend off our list. Let’s say that we want a three day weekend so we want Friday, Saturday and Sunday. The first number is the index of our first element that we want to include and the second is the element that we don’t want to include. This is a bit confusing but just think of the last number being the index of the item you don’t want to include to your list.

`weekend = days_of_week[4:7]`

Shortcuts:

If we want to do a slice at the beginning of the list we can omit the starting index

`weekday = days_of_week[:5]`

If we want to slice data and include the end of our list we can omit the last number

`weekend = days_of_week[5:]`

Membership Operators

Membership Operators are the operators, which are used to check whether a value/variable exists in the sequence like string, list, tuples, sets, dictionary or not.

These operator returns either True or False, if a value/variable found in the list, its returns True otherwise it returns False.

Examples

```'Cat' in 'Cat in the hat was one of my favorite books'
-> True

'cat' in 'Cat in the hat was one of my favorite books'
-> False

3 not in [1, 2, 3, 4,5]
-> True

100 in [1, 2, 3, 4]
-> False```

Notes: List elements can be changed by doing something like

```days_of_week[1] = "No Monday"
days_of_week = ['No Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday']```

If only it were that easy to get rid of Mondays.

List Methods

len() — Returns how many elements are in a list. (length)

max() — Returns the greatest element in the list. This can be the largest number of the last of the items sorted alphabetically.  This will not work with a list of mixed elements.

min() — Returns the smallest element in the list.

sorted() — Returns a copy of the list but sorted from smallest to largest. The optional argument of reverse=True can be added to sort from largest to smallest.

`sorted(ages, reverse=True)`

join() — To be used on a list of strings. It will return a new string combining the elements in the list. We will get an error if we try to join anything other than strings.

We need to tell it what we want to join the elements with at the begnining of the method.

```my_list = ["car", "airplane", "train"]
"-".join(my_list)
-> car-airplane-train```

append() — Will add an element to the end of the list.

Other useful information

Strings are not mutable they are immutable meaning they can’t be changed so when we create a variable with a string

`name = "Claudia"`

and we assign that variable to another variable

```mom = name
-> mom = "Claudia"
-> name = "Claudia"```

if we change the value of name the value of mom stays the same

```name = "Claudia Lilia"
-> mom = "Claudia"
-> name = "Claudia Lilia"```
```however the same is not the same with mutable data like lists
ages = [39, 38, 11, 5, 13]```

if we copy that list over to a new one

```family_ages = ages
-> ages = [39, 38, 11, 5, 13]
-> family_ages = [39, 38, 11, 5, 13]```

Now if we make a change to the original list

`ages[3] = 6`

both lists change to reflect that change.

```-> ages = [39, 38, 11, 6, 13]
-> family_ages = [39, 38, 11, 6, 13]```