<jupyter_start><jupyter_text>For Loops (2) - Looping through the items in a sequence In the last lesson we introduced the concept of a For loop and learnt how we can use them to repeat a section of code.  We learnt how to write a For loop that repeats a piece of code a specific number of times using the range() function, and saw that we have to create a variable to keep track of our position in the loop (conventionally called i). We also found out how to implement if-else statements within our loop to change which code is run inside the loop.As well as writing a loop which runs a specific number of times, we can also create a loop which acts upon each item in a sequence. In this lesson we'll learn how to implement this functionality and find out how to use this knowledge to help us make charts with Plotly. Looping through each item in a sequenceBeing able to access each item in turn in a sequence is a really useful ability and one which we'll use often in this course. The syntax is very similar to that which we use to loop through the numbers in a range:```` pythonfor  in :    ````The difference here is that the variable which keeps track of our position in the loop does not increment by 1 each time the loop is run. Instead, the variable takes the value of each item in the sequence in turn:<jupyter_code>list1 = ['a', 'b', 'c', 'd', 'e']

for item in list1:
    print(item)<jupyter_output>a
b
c
d
e<jupyter_text>It's not important what we call this variable:<jupyter_code>for banana in list1:
    print(banana)<jupyter_output>a
b
c
d
e<jupyter_text>But it's probably a good idea to call the variable something meaningful:<jupyter_code>data = [20, 50, 10, 67]

for d in data:
    print(d)<jupyter_output>20
50
10
67<jupyter_text>Using these loopsWe can use these loops in conjunction with other concepts we have already learnt. For example, imagine that you had a list of proportions stored as decimals, but that you needed to create a new list to store them as whole numbers.We can use list.append() with a for loop to create this new list. First, we have to create an empty list to which we'll append the percentages:<jupyter_code>proportions = [0.3, 0.45, 0.99, 0.23, 0.46]

percentages = []<jupyter_output><empty_output><jupyter_text>Next, we'll loop through each item in proportions, multiply it by 100 and append it to percentages:<jupyter_code>for prop in proportions:
    percentages.append(prop * 100)
    
print(percentages)<jupyter_output>[30.0, 45.0, 99.0, 23.0, 46.0]<jupyter_text>Using for loops with dictionariesWe've seen how to loop through each item in a list. We will also make great use of the ability to loop through the keys and values in a dictionary.If you remember from the dictionaries lessons, we can get the keys and values in a dictionary by using dict.items(). We can use this in conjunction with a for loop to manipulate each item in a dictionary. This is something which we'll use often; we'll often have data for several years stored in a dictionary; looping through these items will let us plot the data really easily.In the cell below, I've created a simple data structure which we'll access using a for loop. Imagine that this data contains sales figures for the 4 quarters in a year:<jupyter_code>data = {2009 : [10,20,30,40],
       2010 : [15,30,45,60],
       2011 : [7,14,21,28],
       2012 : [5,10,15,20]}<jupyter_output><empty_output><jupyter_text>We can loop through the keys by using dict.keys():<jupyter_code>for k in data.keys():
    print(k)<jupyter_output>2009
2010
2011
2012<jupyter_text>And we can loop through the values (which are lists):<jupyter_code>for v in data.values():
    print(v)<jupyter_output>[10, 20, 30, 40]
[15, 30, 45, 60]
[7, 14, 21, 28]
[5, 10, 15, 20]<jupyter_text>We can loop through them both together:<jupyter_code>for k, v in data.items():
    print(k, v)<jupyter_output>2009 [10, 20, 30, 40]
2010 [15, 30, 45, 60]
2011 [7, 14, 21, 28]
2012 [5, 10, 15, 20]<jupyter_text>Having the data available to compare each year is really handy, but it might also be helpful to store them as one long list so we can plot the data and see trends over time. First, we'll make a new list to store all of the data items:<jupyter_code>allYears = []<jupyter_output><empty_output><jupyter_text>And then we'll loop through the dictionary and concatenate each year's data to the allYears list:<jupyter_code>for v in data.values():
    allYears = allYears + v
    
print(allYears)<jupyter_output>[10, 20, 30, 40, 15, 30, 45, 60, 7, 14, 21, 28, 5, 10, 15, 20]
{'fraction_non_alphanumeric': 0.07546337157987644, 'fraction_numerical': 0.04964695498676081, 'mean_word_length': 4.555147058823529, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '2392', 'license': 'MIT', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1355, 'n_words': 693, 'path': 'PlotlyandPython/Lessons/(01) Intro to Python/Notebooks/(12) For Loops (2) - Looping through the items in a sequence.ipynb', 'repo_name': 'peternewman22/Python_Courses', 'size': '4532'}