<jupyter_start><jupyter_text>2D Lists Indexing**Remember:** - Lists can be used to group different values together - it's just a collection of things.- You can make a list in Python by putting different things in a box of brackets `[]` separated by commas- You can make lists of lists, which are called 2D listsHere we're going to learn how to index lists of lists. Let's recreate our 2D list of `food`:<jupyter_code># create 2D list
food = [['apple', 'banana', 'grape', 'mango'], 
        ['lettuce','carrot','cucumber','beet'], 
        ['chicken', 'beef', 'fish', 'pork']]

# print 2D list along with its length
print(food)
print(len(food))<jupyter_output>[['apple', 'banana', 'grape', 'mango'], ['lettuce', 'carrot', 'cucumber', 'beet'], ['chicken', 'beef', 'fish', 'pork']]
3<jupyter_text>Like we saw in the last lesson, the length of the `food` list (3) is the number of 1D lists that make up the 2D list. The fruit list is the first element in `food`. To get this first element from `food`, we do it the same way we would in a 1D list:<jupyter_code># get the fruit list from the food list
food[0]<jupyter_output><empty_output><jupyter_text>In this way, we can think of the list `food` as a 1D list, where each element is another list. As we saw in the last command, we can reference the first sublist from `food` using `food[0]`.Knowing this, how can we get the length of the first list within `food`?<jupyter_code># print length of the first index of food
len(food[0])<jupyter_output><empty_output><jupyter_text>Thinking back to indexing 1D lists, how do you think we can get the lists fruit and vegetables from the list `food`?<jupyter_code># get fruit and vegetables from the list
food[0:2]<jupyter_output><empty_output><jupyter_text>What if we want to get the value `'mango'` from our list? We can first save the first element from `food` to a variable `fruit`, and then index the `fruit` list:<jupyter_code># get the fruit list from food and save it in the variable fruit
fruit = food[0]

# print the fruit list to make sure you got what you wanted
print(fruit)

# get mango from the fruit list
fruit[3]<jupyter_output>['apple', 'banana', 'grape', 'mango']<jupyter_text>We can also get the value `'mango'` from our list in one line! To do this, we can just index `food` twice. The first index is to get the fruit list, and the second index is to get `'mango'` from the fruit list:<jupyter_code># get mango by indexing the food list twice
food[0][3]<jupyter_output><empty_output><jupyter_text>How can you get `'carrot'` from the `food` 2D list?<jupyter_code># get carrot from the food 2D list
food[1][1]<jupyter_output><empty_output><jupyter_text>Now, try getting `'fish'` and `'pork'` from the `food` 2D list:<jupyter_code># get fish and pork from the food 2D list
food[2][2:4]<jupyter_output><empty_output><jupyter_text>Finally, let's make a new list called `favorites` with your favorite food within each list in `food`. Print it out afterwards to make sure you did what you want!<jupyter_code># make a list of your favorite fruit, vegetable, and meat
favorites = [food[0][3], food[0][2], food[2][1]]

# print favorite
print(favorites)<jupyter_output>['mango', 'grape', 'beef']
{'fraction_non_alphanumeric': 0.1121905358821686, 'fraction_numerical': 0.011281729865246004, 'mean_word_length': 5.448484848484848, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '39403', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1038, 'n_words': 451, 'path': 'Lessons/_Keys/KEY_Lesson07_2D_Lists_Indexing.ipynb', 'repo_name': 'lechten/curriculum-notebooks', 'size': '3191'}