Lists, Tuples, & Dictionaries

This lesson will teach you how to use lists, tuples, and dictionaries to store data.

Lists

Lists are simply containers we can fill with objects.

These objects can be anything (Strings, Numbers, Variables, even Turtles!)

We can create a list by putting one or more things between square brackets [ ].

Each item in the list should be separated with a comma ,. It's good form to have a space after every comma to make your code more readable.

```word = "Bye"

items = ["Hi", word, 5, True]

print(items)

# Output:
# ['Hi', 'Bye', 5, True]
```

Lists can be changed after they have been created. This is called mutability.

We can add to the end of a list by using the append() function.

```word = "Bye"

items = ["Hi", word, 5, True]

items.append("lists are cool")

print(items)

# Output:
# ['Hi', 'Bye', 5, True, 'lists are cool']
```

We can take things out of a list using the remove() function. This function will only remove what you tell it to rather than taking from the end of the list.

```word = "Bye"

items = ["Hi", word, 5, True]

items.append("lists are cool")

print(items)

# Output:
# ['Hi', 'Bye', 5, True, 'lists are cool']

items.remove('Hi')

print(items)

# Output:
# ['Bye', 5, True, 'lists are cool']
```

We can get an element at a specific location of the list through indexing.

An index is an item's position in a list. Imagine you have a line of people. The first person in the line would be standing at index 0. The second person would be standing at index 1, and the third person would be standing at index 2. This continues for your entire line of people, or list of items. Remember that indexes always start at 0!

So to select a particular item from a list, simply write the name of the list, then the index of the desired item in square brackets. We can then use this item or even change it.

Remember: Indexes start at 0, so getting the item at position 0 will get you the first item in the list.

```items = ["Hi", "Bye", 5]

print(items[0])
print(items[1])
print(items[2])

# Output:
# Hi
# Bye
# 5

items[2] = 20
print(items[2])

# Output:
# 20
```

We can also check if an item is in or not in a list using if statements.

```items = ["Hi", "Bye", 5]

if 5 in items:
print("list contains 5")
else:
print("list doesn't contain 5")

if "Hello" not in items:
print("list doesn't contain hello")

# Output:
# list contains 5
# list doesn't contain hello
```

Tuples

Tuples are just like lists, but we use parentheses ( ) to make them.

Tuples also cannot be changed after they are created. This is called immutability. This means that the contents of the Tuple cannot be changed and the length of the Tuple cannot increase or decrease.

```number = 8

myTuple = ("Hi", "Bye", 6, True, number)

print(myTuple)

# Output:
# ('Hi', 'Bye', 6, True, 8)
```

Tuples maintain a lot of the functions of lists.

You can get elements inside of a tuple by using indexing just like a list.

```number = 8

myTuple = ("Hi", "Bye", 6, True, number)

print(myTuple[1])

# Output:
# Bye
```

If you want to create a tuple with only one item, you must have a comma at the end. Otherwise Python will not know it is a tuple.

The type() function allows us to see the data type of a variable.

```#tuple
single1 = ("item1",)

#not a tuple
single2 = ("item2")

print(type(single1))
print(type(single2))

# Output:
# class 'tuple'
# class 'str'
```

Dictionaries

Dictionaries are like lists but have a key-value format.

Dictionary structures work like a real dictionary that has a word and a definition for that word.

Dictionaries are created with curly brackets { } and each item consists of a string to define the key, then a colon :, and then a value.

If you place multiple key-value pairs in the dictionary, they are separated with a comma ,.

Values can be accessed like indexing with lists, but instead of typing a number we use the key name instead.

```dict = {"One" : 1, "Two" : 2, "Three" : 3}

print(dict["One"])
print(dict["Two"])
print(dict["Three"])

# Output:
# 1
# 2
# 3
```

Dictionaries are very useful for storing pairs of data.

You can change the value for a Dictionary key after the Dictionary has been created.

New entries can be added the same way we assign new values to existing keys.

You may also remove entries using the del command.

```dict = {"One" : 1, "Two" : 2, "Three" : 3}
print(dict)
# Output: {"One" : 1, "Two" : 2, "Three" : 3}

# Modifies an existing Key-value pair
dict["Two"] = 99
print(dict)
# Output: {'One': 1, 'Two': 99, 'Three': 3}

# Removes an existing Key-value pair
del dict["One"]
print(dict)
# Output: {'Two': 99, 'Three': 3}

# Adds a new Key-value pair
dict["Four"] = 4
print(dict)
# Output: {'Two': 99, 'Three': 3, 'Four': 4}
```

Unlike lists, dictionaries do not have a specific order. While you might add key-value pairs to a dictionary in a specific order, you don't really control where in the dictionary it will go.