<jupyter_start><jupyter_text>Storing Multiple Values in Lists Just as a `for` loop is a way to do operations many times,a list is a way to store many values.Unlike NumPy arrays,lists are built into the language (so we don't have to load a libraryto use them).We create a list by putting values inside square brackets and separating the values with commas:<jupyter_code>odds = [1,3,5,7]
odds<jupyter_output><empty_output><jupyter_text>We select individual elements from lists by indexing them:<jupyter_code>weird = ["bob",5,5.8,[1,2,3],sum]
odds<jupyter_output><empty_output><jupyter_text>and if we loop over a list,the loop variable is assigned elements one at a time:<jupyter_code>odds = [1,3,5,7]
odds[2]<jupyter_output><empty_output><jupyter_text>There is one important difference between lists and strings:we can change the values in a list,but we cannot change individual characters in a string.For example:<jupyter_code>for o in weird:
    print(o)<jupyter_output>bob
5
5.8
[1, 2, 3]
<built-in function sum><jupyter_text>works, but:<jupyter_code>odds[2] = 17<jupyter_output><empty_output><jupyter_text>does not.<jupyter_code>odds<jupyter_output><empty_output><jupyter_text>Ch-Ch-Ch-Ch-ChangesData which can be modified in place is called mutable,while data which cannot be modified is called immutable.Strings and numbers are immutable. This does not mean that variables with string or number valuesare constants, but when we want to change the value of a string or number variable, we can onlyreplace the old value with a completely new value.Lists and arrays, on the other hand, are mutable: we can modify them after they have beencreated. We can change individual elements, append new elements, or reorder the whole list. Forsome operations, like sorting, we can choose whether to use a function that modifies the datain-place or a function that returns a modified copy and leaves the original unchanged.Be careful when modifying data in-place. If two variables refer to the same list, and you modifythe list value, it will change for both variables!<jupyter_code>evens = odds<jupyter_output><empty_output><jupyter_text>If you want variables with mutable values to be independent, youmust make a copy of the value when you assign it.<jupyter_code>evens[1] = 200<jupyter_output><empty_output><jupyter_text>Because of pitfalls like this, code which modifies data in place can be more difficult tounderstand. However, it is often far more efficient to modify a large data structure in placethan to create a modified copy for every small change. You should consider both of these aspectswhen writing your code.<jupyter_code>evens
odds<jupyter_output><empty_output><jupyter_text>Nested ListsSince lists can contain any Python variable, it can even contain other lists.For example, we could represent the products in the shelves of a small grocery shop: Here is a visual example of how indexing a list of lists `x` works:[![The first element of a list. Adapted from @hadleywickham.](https://pbs.twimg.com/media/CO2_qPVWsAAErbv.png:large)](https://twitter.com/hadleywickham/status/643381054758363136)Using the previously declared list `x`, these would be the results of theindex operations shown in the image:*Thanks to [<NAME>](https://twitter.com/hadleywickham/status/643381054758363136) for the image above.*  Heterogeneous ListsLists in Python can contain elements of different types. Example:```pythonsample_ages = [10, 12.5, 'Unknown']``` There are many ways to change the contents of lists besides assigning new values toindividual elements: While modifying in place, it is useful to remember that Python treats lists in a slightlycounter-intuitive way.If we make a list and (attempt to) copy it then modify in place, we can cause all sorts of trouble: This is because Python stores a list in memory, and then can use multiple names to refer to thesame list. If all we want to do is copy a (simple) list, we can use the `list` function, so we donot modify a list we did not mean to: This is different from how variables worked in lesson 1, and more similar to how a spreadsheetworks.  Challenge: Turn a String Into a ListUse a for-loop to convert the string "hello" into a list of letters:[&quot;h&quot;, &quot;e&quot;, &quot;l&quot;, &quot;l&quot;, &quot;o&quot;]Hint: You can create an empty list like this:my_list = []  Solution Subsets of lists and strings can be accessed by specifying ranges of values in brackets,similar to how we accessed ranges of positions in a NumPy array.This is commonly referred to as "slicing" the list/string.  Challenge: Slicing From the EndUse slicing to access only the last four characters of the following string and the last four entries of the following list. Would your solution work regardless of whether you knew beforehandthe length of the string or list(e.g. if you wanted to apply the solution to a set of lists of different lengths)?If not, try to change your approach to make it more robust.  SolutionUse negative indices to count elements from the end of a container (such as list or string):  Non-Continuous SlicesSo far we've seen how to use slicing to take single blocksof successive entries from a sequence.But what if we want to take a subset of entriesthat aren't next to each other in the sequence?You can achieve this by providing a third argumentto the range within the brackets, called the _step size_.The example below shows how you can take every third entry in a list: Notice that the slice taken begins with the first entry in the range,followed by entries taken at equally-spaced intervals (the steps) thereafter.If you wanted to begin the subset with the third entry,you would need to specify that as the starting point of the sliced range:  Challenge:Use the step size argument to create a new stringthat contains only every other character in the string"In an octopus's garden in the shade"  SolutionTo obtain every other character you need to provide a slice with the stepsize of 2: You can also leave out the beginning and end of the slice to take the whole stringand provide only the step argument to go every secondelement: If you want to take a slice from the beginning of a sequence, you can omit the first index in therange: And similarly, you can omit the ending index in the range to take a slice to the very end of thesequence:  Overloading`+` usually means addition, but when used on strings or lists, it means "concatenate".Given that, what do you think the multiplication operator `*` does on lists?In particular, what will be the output of the following code?```pythoncounts = [2, 4, 6, 8, 10]repeats = counts * 2print(repeats)```1.  `[2, 4, 6, 8, 10, 2, 4, 6, 8, 10]`2.  `[4, 8, 12, 16, 20]`3.  `[[2, 4, 6, 8, 10],[2, 4, 6, 8, 10]]`4.  `[2, 4, 6, 8, 10, 4, 8, 12, 16, 20]`The technical term for this is *operator overloading*:a single operator, like `+` or `*`,can do different things depending on what it's applied to.  SolutionThe multiplication operator `*` used on a list replicates elements of the list and concatenatesthem together:<jupyter_code>counts = [2,4,6,8,10]
repeats = counts*2
repeats<jupyter_output><empty_output><jupyter_text>It's equivalent to:<jupyter_code>repeats = counts + counts
repeats<jupyter_output><empty_output><jupyter_text>---The material in this notebook is derived from the Software Carpentry lessons&copy; [Software Carpentry](http://software-carpentry.org/) under the termsof the [CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/) license.<jupyter_code>col = ["c0","c1","c3"]
for colour in col*2:
    print(colour)<jupyter_output>c0
c1
c3
c0
c1
c3
{'fraction_non_alphanumeric': 0.06926121372031663, 'fraction_numerical': 0.01978891820580475, 'mean_word_length': 5.333333333333333, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 4, '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': '10603', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2106, 'n_words': 1066, 'path': '03-fundamentals-of-python/03-lists.ipynb', 'repo_name': 'noushinkarim/rcsc18_lessons', 'size': '7588'}