# Loops and Lists

{% embed url="<https://youtu.be/ZkfOf5PAU94>" %}

&#x20;[Data Structures](http://python.swaroopch.com/data_structures.html)

## Working with Lists

We don't typically just create lists with all of the values in them. Usually we get the lists dynamically (meaning we get them when the program is running either from a user, a file, the Internet or somewhere else). This means that we don't always know very much about our lists such as how big they are or what the values are inside the list. For this reason we might want to loop over all of the items in the list.

```python
my_list = [1, 2, 3, 4, 5]
for num in my_list:
  print(num)
```

This looks almost like plain English and can hardly qualify as code right? The value `num` in the loop declaration will be the value of the current item in the list. The loop will replace its value each iteration to be the current value.

Sometimes we want to know the index of the item in the list that we are looping over. You can do that with the enumerate function:

```python
for i, num in enumerate(my_list):
  print(i, num)
```

This time we have two variables: `i` and `num`. The `i` variable will be the index and the `num` variable will be the current item in the list.

## Dictionaries

One thing to keep in mind with dictionaries is that the pairs in the dictionary are not ordered. When you enter items into a list the items exist in the order that you added them but this is not the case with a dictionary. Dictionaries use a very efficient operation to store and retrieve data which makes it so they are not ordered like you would expect.

You can still iterate over dictionary though. This looks very similar to using a list.

```python
for key in my_dict:
  print("the value is: %s".format(my_dict[key]))
```

To iterate over the keys and values at the same time you can use the `items()` method.

```python
for key, value in my_dict.items():
  print("key: %s, value: %d".format(key, value))
```

## Lists of lists or 2D arrays/lists

So far, we have put numbers and strings into lists and we could imagine that they can also hold booleans right? But what if we wanted to have a list hold ... another list? Wouldn't that be cool! We call this a two-dimensional array (or list). We could print out a Tic-Tac-Toe game using a two dimensional list:

```python
def print_2d(l):
  for i in l:
    for j in i:
      print(j),
    print

my_list = [ ['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-'] ]

print_2d(my_list)

my_list[1][1] = 'x'

print_2d(my_list)
```

&#x20;*Try printing out a full Tic-Tac-Toe board (instead of the simple one I just did) using 2D arrays!*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programming.reedcwilson.com/lesson-3/loops-and-lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
