# Data Structures

Hey there, welcome back to the Python basics tutorial! Today I'm going to be teaching you about Data Structures.

If you're freaking out by the name "Data Structures", calm down. We're not going to be messing with huge numbers here.

A data structure is a way to store, organize and manipulate your data.

## Lists

&#x20;Let's say you have to store the usernames of 5 people. Sure, you can create 5 variables for each user, but if there was a new user? Then you'd have to figure out how to make a new variable with the users name from a string. A simpler way here would be to make a "list" of usernames. How do you make a list?

```python
users = ['John', 'Jake', 'Jack', 'Joe']
```

We're defining a list by putting values separated by commas inside a pair of square brackets. We can store integers too:

```python
apple_prices = [30, 21, 45]
```

Or, you can mix them:

```python
random_values = ['Logan', 31, 'Jake', 12, 'Paul', 26]
```

A list is a **data structure**.

\
You can access values stored in a list by mentioning its place or *index* in the list. You access them by using their index inside a pair of square brackets as well.

&#x20;Important thing you should *always* remember: List indexes start from 0, not from 1, as you might expect. So, the first element in a list is actually the 0th element. It might seem confusing, so let's see it being used.

```python
>>> users = ['Jake', 'Jack', 'Joe', 'John']
>>> first = users[0] # We're taking the first element of users and assigning to first.
>>> print(first)
'Jake'
>>> print(users[1])
'Jack'
```

Easy enough. You can also change the value of a list's element by their index:

```python
>>> users = ['John', 'Jack']
>>> print(users)
['John', 'Jack']
>>> users[1] = 'Joe'
>>> print(users)
['John', 'Joe']
```

You can "nest" or put a list in another.

```python
>>> nested_list = ['one', ['two', 'three', ['four']]]
>>> # access them like this:
>>> print(nested_list[1][0])
'three'
>>> print(nested_list[1][2][0]
'four'
```

You can create empty lists too:

```python
empty_list = []
```

You'll learn more about lists later in the chapter.

{% hint style="info" %}
Lists are called arrays in other programming languages.
{% endhint %}

A list is only one data structure. There are a few more, tuples, dictionaries and sets.

## Tuples

Tuples are a lot like lists, except they're constant. You can only create a tuple, but not modify it. You can index a tuple just like lists, but assigning won't work. You create a tuple just like a list, but with parentheses ().

```python
>>> users = ('Logan', 'Wade')
>>> print(users[0])
'Logan'
>>> users[0] = 'Tony'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> print(users)
('Logan', 'Wade')
```

## Dictionaries

So far, you've been indexing lists and tuples via numbers. What if you wanted to get the age of a user named 'Mike' from a data structure? It would be hard to manually specify the indexes, so you can use dictionaries. They're created with {curly braces} and have a key == value structure. You can access it like this: `dictionary[key]` to get `value`.

```python
>>> data = {'John': 21, 'Mike': 36}
>>> print(data['John'])
21
>>> print(data['Mike'])
36
```

Simple. You can also assign values like:

```python
>>> users = {'John': 21}
>>> users['Mike'] = 32
>>> print(users)
{'John': 21, 'Mike': 32}
```

You can also nest dictionaries.

```python
user_data = {'Mike': {'age': 21, 'balance': 36}}
# and access it like this:
user_data['Mike']['age']
```

## Sets

Sets are yet another kind of data structure. They only store unique values, so you can not store twins called Mike in a set. You can create a set like this:

```python
my_set = set(1, 'hello', 'george', 21)
print(my_set)
```

But if you add duplicate elements:

```python
users = set('George', 'Tom', 'Mike', 'Andrew', 'Mike')
print(users)
```

You'll see that there's only 1 Mike. Poor twins.

## Which one should I use, and when?

When you just want a list of values, well, use a `list`. If you want a constant list, whose values never change, use tuples. When you need to access values by specific keys, go for dictionaries. Sets are more "memory efficient", so when you have all-unique values and need speed, use sets.

{% hint style="info" %}
In most cases you'll probably want to use a list.
{% endhint %}

## List operations

You must be thinking, we really can just store and access data from lists and tuples. And adding values with `list[index] = value` isn't really great. Luckily, Python has a BUNCH of useful operations we can use to manipulate lists. Suppose you have to add a username to the end of your list. You can manually find out the next index and assign the value to that, OR you can use `append()`:

```python
>>> users = ['Joe']
>>> users.append('Jack')
>>> print(users)
['Joe', 'Jack']
```

MUCH better, right? You can use `len()` to find the length of lists, tuples, dictionaries and sets:

```python
>>> fruits = ['Apples', 'Oranges', 'Harry Potter']
>>> length = len(fruits)
>>> print(length)
3
```

{% hint style="info" %}
`len()` can be used on strings too.
{% endhint %}

You can use `index()` to find the index of a specific value in a list.

```python
>>> goldfishes = ['Jeremy', 'Goldilocks', 'Thor']
>>> print(goldfishes.index('Thor'))
2
```

Or `insert()` to insert an element at a given index:

```python
>>> monsters = ['zombies', 'skeletons']
>>> monsters.insert(1, 'boogeyman') # Inserts 'boogeyman' as the 1st element.
>>> print(monsters)
['zombies', 'boogeyman', 'skeletons']
```

And `remove()` to remove the first match of the given element:

```python
>>> colors = ['red', 'green', 'blue']
>>> colors.remove('green')
>>> print(colors)
['red', 'blue']
```

And `pop()` to remove values at a specific index:

```python
>>> weapons = ['sword', 'club', 'no u']
>>> weapons.pop(1)
>>> print(weapons)
['sword', 'no u']
```

Let's not forget:

`reverse()` - Reverse the list.

```python
>>> food = ['meat', 'fish', 'memes'].reverse()
>>> print(food)
['memes', 'fish', 'meat']
```

`sort()` - Sort the list.

```python
>>> grades = [32, 12, 73, 8].sort()
>>> print(grades)
[8, 12, 32, 73]
```

`max()` and `min()` to get the maximum and minimum values of a list respectively.

```python
>>> grades = [3204, 654, 1228]
>>> print(max(grades))
3204
>>> print(min(grades))
654
```

We can use `.join()` to join the elements of a list together with a specified delimiter or separator:

```python
>>> people = ['Mark', 'Tony', 'Peter']
>>> new_people = ','.join(people)
>>> print(new_people)
Mark,Tony,Peter
```

It's used like: `string.join(list)` and joins the elements of the list together with `string` separating them.

{% hint style="info" %}
You must've noticed that sometimes we do `list.function()` and sometimes `function(list)`. In the first one, `function` is actually a method. A method is actually a regular function "bound" to an `object` such as a list. (You'll learn more about this in next chapters.) And the second one is just a regular function.
{% endhint %}

We can use the `in` logical operator to determine if an element is in a list:

```python
people = ['Mark', 'Mike', 'Mjolnir']
username = input('Username: ')
if username in people:
    print('Welcome!')
else:
    print('vjnrjfnfjvnfjvnjmjmvrjkr')
```

{% hint style="info" %}
We can use `in` with strings too.

```python
>>> 'hello' in 'World, i say hello.'
True
```

{% endhint %}

We've barely scratched the surface! There are TONS of other cool stuff you can do with lists, but that's your homework.

## Conversion between data structures

Another important thing I should say before ending this chapter: How to convert between data structure types? Well, that sure is easy enough.

To convert something to a list: `list(other)`\
To convert to a tuple: `tuple(other)`\
Converting to a dictionary from a list or tuple isn't that simple, so we'll look at it later.\
To convert to a set: `set(other)`&#x20;

Whew! That was a long chapter, but I hope you've got the gist of data structures, why and how to use them! If you face any problems, try to google it first. If that doesn't work, you can always contact me!
