<jupyter_start><jupyter_text>Variable Types - Lists (2) In the last lesson we learnt that a list can hold any number of  variables and that these variables can be of any type, even other lists. We also saw that we can index and slice lists in the same way that we can with strings.In this lesson we'll learn how to add items to a list using the list.append() function, how to delete an item from a list by its index using the del keyword, and how to create and access nested lists.  Adding items to a listWhen Plotly interprets data, it expects each particular piece (or trace) of data to be an item in a list, for example Plotly would expect the x-values and y-values of a chart to be in two separate lists, and there are many occasions where we would want to create the dynamically.To do this, we use the list.append() function.First, we start with an empty list which will eventually contain our x-values:<jupyter_code>xVals = []

print(xVals)<jupyter_output>[]<jupyter_text>Then, we might decide that we want to add some observations to our list of x-values. To do this, we can use list.append() to add them one by one (we'll learn a faster way to do this in a future lesson):<jupyter_code>xVals.append(2)

print(xVals)

xVals.append(4)

print(xVals)

xVals.append(6)

print(xVals)

xVals.append(8)

print(xVals)

xVals.append(10)

print(xVals)<jupyter_output>[2]
[2, 4]
[2, 4, 6]
[2, 4, 6, 8]
[2, 4, 6, 8, 10]<jupyter_text>Deleting items from a listAfter creating our list of x-values, we might then want to delete items from that list. To do this, we use the del keyword in conjunction with the list index to select the item which we want to remove.Here I'm taking a copy of the x-vals because I want to use this list later on in this lesson:<jupyter_code>xValsDel = xVals<jupyter_output><empty_output><jupyter_text>If we wanted to delete the first (or zeroeth) item in the list we would use the del keyword, and select the item at index 0:<jupyter_code>print("Before:", xValsDel)

del xValsDel[0]

print("After:", xValsDel)<jupyter_output>Before: [2, 4, 6, 8, 10]
After: [4, 6, 8, 10]<jupyter_text>We might also want to delete the last item in the list. We can select the last item by choosing the item at index -1:<jupyter_code>print("Before:", xValsDel)

del xValsDel[-1]

print("After:", xValsDel)<jupyter_output>Before: [4, 6, 8, 10]
After: [4, 6, 8]<jupyter_text>Creating and accessing nested listsSo, now we've created our x-values and y-values and seen how to delete items from them as we wish, we'll now learn how to create a nested list and how to access the items in that list.First, I'll create a list which contains our x-values and y-values:<jupyter_code>yVals = [1,2,3]
allData = [xVals, yVals]
print(allData)<jupyter_output>[[4, 6, 8], [1, 2, 3]]<jupyter_text>We can then access the item in the zeroeth position by using index 0:<jupyter_code>print(allData[0])<jupyter_output>[4, 6, 8]<jupyter_text>We can then access the items inside the list at index 0 in allData by adding another square bracket:<jupyter_code>print(allData[0][1])<jupyter_output>6<jupyter_text>We can continue this process indefinitely, with nested lists inside nested lists. This list is three levels deep:<jupyter_code>allData2 = [allData, allData]
allData2<jupyter_output><empty_output><jupyter_text>We can access the list at index 0:<jupyter_code>allData2[0]<jupyter_output><empty_output><jupyter_text>We can access the nested list at index 1:<jupyter_code>allData2[0][1]<jupyter_output><empty_output><jupyter_text>And we can access the item at index 2 in that list:<jupyter_code>allData2[0][1][2]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08749654982059067, 'fraction_numerical': 0.02152911951421474, 'mean_word_length': 5.483005366726297, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '157935', 'license': 'MIT', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1209, 'n_words': 504, 'path': 'PlotlyandPython/Lessons/(01) Intro to Python/Notebooks/(07) Variable Types - Lists (2).ipynb', 'repo_name': 'peternewman22/Python_Courses', 'size': '3623'}